showing o/p in c: Error: Command failed: timeout 7 ./HelloWorld
#include<stdio.h>
int main()
{
int a,b;
printf("enter a: ");
scanf("%d",&a);
printf("enter b: ");
scanf("%d",&b);
printf("addition is %d",a+b);
return 0;
}
3 Answers
You need to make sure, you provide STDIN as the program is taking two inputs.
Otherwise the program will wait and timeout eventually
Check this https://onecompiler.com/c/3x8wmck9r
Update: Unfortunately OneCompiler does not show as you are expecting. I have updated the code to show it in best possible way.
Thanx a lot,for Ur very very kind and fast help and answer.
But, i want to input from kb like below:
enter a: 5
enter b: 10
addition is 15
Thanx a lot.
#include <iostream>
#include <unordered_map>
#include <limits>
#include <string>
using namespace std;
// Structure to store account details
struct Account {
string password;
double balance;
};
// Global map to store user accounts
unordered_map<string, Account> accounts;
// Function to clear input stream
void clearInputStream() {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
// Function to create a new bank account
void createAccount() {
string userId, password;
cout << "Enter a new user ID: ";
cin >> userId;
if (accounts.find(userId) != accounts.end()) {
cout << "User ID already exists. Please choose a different ID." << endl;
return;
}
cout << "Enter a new password: ";
cin >> password;
accounts[userId] = {password, 0.0};
cout << "Account created successfully!" << endl;
}
// Function to login with user ID and password
string login() {
string userId, password;
cout << "Enter your user ID: ";
cin >> userId;
auto it = accounts.find(userId);
if (it == accounts.end()) {
cout << "User ID not found. Please try again." << endl;
return "";
}
cout << "Enter your password: ";
cin >> password;
if (it->second.password == password) {
cout << "Login successful!" << endl;
return userId;
} else {
cout << "Incorrect password. Please try again." << endl;
return "";
}
}
// Function to withdraw money from account
void withdrawMoney(Account& acc) {
double amount;
cout << "Enter the amount to withdraw: ";
cin >> amount;
if (amount > acc.balance) {
cout << "Insufficient funds." << endl;
} else {
acc.balance -= amount;
cout << "Withdrawal successful. New balance: $" << acc.balance << endl;
}
}
// Function to deposit money into account
void depositMoney(Account& acc) {
double amount;
cout << "Enter the amount to deposit: ";
cin >> amount;
acc.balance += amount;
cout << "Deposit successful. New balance: $" << acc.balance << endl;
}
// Function to request balance of the account
void requestBalance(const Account& acc) {
cout << "Your current balance is: $" << acc.balance << endl;
}
// Main menu function for logged-in users
void mainMenu(const string& userId) {
while (true) {
cout << "\nMain Menu:" << endl;
cout << "1) Withdraw money" << endl;
cout << "2) Deposit money" << endl;
cout << "3) Request balance" << endl;
cout << "4) Quit" << endl;
cout << "Choose an option: ";
int choice;
cin >> choice;
clearInputStream(); // Clear input stream to handle invalid input
auto it = accounts.find(userId);
if (it == accounts.end()) {
cout << "User account not found!" << endl;
return;
}
Account& acc = it->second;
switch (choice) {
case 1:
withdrawMoney(acc);
break;
case 2:
depositMoney(acc);
break;
case 3:
requestBalance(acc);
break;
case 4:
cout << "Quitting..." << endl;
return;
default:
cout << "Invalid choice. Please try again." << endl;
}
}
}
// Introduction menu function
void introductionMenu() {
while (true) {
cout << "\nIntroduction Menu:" << endl;
cout << "1) Create a bank account" << endl;
cout << "2) Login" << endl;
cout << "3) Quit" << endl;
cout << "Choose an option: ";
int choice;
cin >> choice;
clearInputStream(); // Clear input stream to handle invalid input
switch (choice) {
case 1:
createAccount();
break;
case 2:
{
string userId = login();
if (!userId.empty()) {
mainMenu(userId);
}
}
break;
case 3:
cout << "Quitting..." << endl;
return;
default:
cout << "Invalid choice. Please try again." << endl;
}
}
}
// Main function
int main() {
introductionMenu();
return 0;
}