### Optional cin

Hello, I'm trying to do a simple calculator in c++ and slowly expand on it
But, at the moment I'm stuck. My code reads:
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566`` ``````#include #include using namespace std; int main() { cout << "\tWelcome to Jacky\'s simple Calculator" << endl; double x, y, z; char u, u2; cin >> x >> u >> y >> u2 >> z; if (u == '+') { if (u2 == '+') { cout << x + y + z << endl; }else if (u2 == '-') { cout << x + y - z << endl; }else if (u2 == '*') { cout << (x + y) * z << endl; }else if (u2 == '/') { cout << (x + y) / z << endl; }else { cout << "Does not fit..." << endl; } }else if (u == '-') { if (u2 == '+') { cout << x - y + z << endl; }else if (u2 == '-') { cout << x - y - z << endl; }else if (u2 == '*') { cout << (x - y) * z << endl; }else if (u2 == '/') { cout << (x - y) / z << endl; }else { cout << "Does not fit..." << endl; } }else if (u == '*') { if (u2 == '+') { cout << (x * y) + z << endl; }else if (u2 == '-') { cout << (x * y) - z << endl; }else if (u2 == '*') { cout << (x * y) * z << endl; }else if (u2 == '/') { cout << (x * y) / z << endl; }else { cout << "Does not fit..." << endl; } }else if (u == '/') { if (u2 == '+') { cout << (x / y) + z << endl; }else if (u2 == '-') { cout << (x / y) - z << endl; }else if (u2 == '*') { cout << (x / y) * z << endl; }else if (u2 == '/') { cout << (x / y) / z << endl; }else { cout << "Does not fit..." << endl; } }else { cout << "Uhh... this doesn\'t work, sorry..." << endl; } return 0; }``````

And it is fine for certain problems.
But what if for the cin
`cin >> x >> u >> y >> u2 >> z;`
They don't want to fill every space? For example, they only want to input "2 + 2" instead of "2 + 2 + 2"
So How would you make a 'cin' that is optional and you can just press the 'enter' instead of having to add "+ 0" at the end of "2 + 2"?
Hi

I have been thinking about this too. I needed a program to add a string of numbers. Since then I have been thinking about how to subtract and multiply, so forth...

BTW< yours does not * or / before it + or -'s so the answer is wrong.

 ``1234`` ``````C:\Temp>testxxx3 Welcome to Jacky's simple Calculator 2+6/3 2.66667 ``````

I will look at this more later, but for now I'll just post my add program. Currently it only adds but, it will add from 2 to millions of numbers as long as they don't total over 2,147,483,647. Maybe you can find something in it to use.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980`` ``````// Add string of whole numbers from command line // Version 2.0 // - added help and added remainder #include #include #include #include using namespace std; int remainder1; // char *s2 = "/?"; void commandhelp () { cout << "Add is a command line calculator to add multiple numbers." << endl; cout << "Counts the number of entries and gives Average/Remainder." << endl; cout << "The maximum size is 2,147,483,647" << endl; cout << "---------------------How to use---------------------" << endl; cout << "Type " << '"' << "add" << '"' << " followed by a string of numbers." << endl; cout << "Example Useage: c:\\> add 1 2 3 4" << endl; cout << "-------------------Aditional Notes------------------" << endl; cout << "These charctres are ignored (A-Z a-z + - / * <> , % \$ # )" << endl; cout << "Using a charcter above that is ignored will increase the " << '"' << "Count of entries" << '"'<< endl; cout << "c:\\> add 1,000 2,000 will result in a total of 3 since everything after the , is ignored" << endl; cout << "Extra spaces are ok, c:\\> add 111 222 555 is the same as c:\\> add 111 222 555." << endl; } int main(int argc, char* argv[]) { // this allows you to pass the values to another function if desired char *s0 = argv[0]; char *s1 = argv[1]; if (argc == 1) { cout << "For Help Type : c:\\> add /?" << endl; cout << "Example Useage: c:\\> add 1 2 3 4" << endl; } else if (argc == 2) { if (!strncmp(s1, "/e", 2)) // if (strcmp (s1,"/e") == 0) { commandhelp(); } else { cout << "For Help Type : c:\\> add /?" << endl; cout << "Example Useage: c:\\> add 1 2 3 4" << endl; } } else if (argc > 2) { string line; int count = 0; // Count of numbers + command add int total = 0; // Sum of numbers while (count+1 <= argc) // zero relative { count = count + 1; // add 1 to ignore command add int x = atoi(argv[count]); // convert to int total = total + x; // while there is another number add one to total } // if total is larger than 2147483647 return value will be negative if (total < 0) { cout << "Error, Number is negative" << endl; } else { remainder1 = (total % (argc-1)); cout << "Total = " << total << endl; cout << "Count of entries) = " << (argc-1) << endl; cout << "Average = " << (total/(count-1)) << " Remainder = " << remainder1 << endl; } } return 0; }``````

Thank you, I will be able to take a more detailed look at your code after I figure out how to run it. Because on line 63
`int x = atoi(argv[count]); // convert to int `
I cannot figure out what you meant "atoi" to be. Seems like a simple syntax error to me
closed account (EAUX92yv)
Try using a bool type variable. Create an if statement that states that if the bool variable is true, take in 2 numbers. Otherwise, take in three. Try experimenting with that. Also, you should use system("CLS") to clear the screen after every problem. Hope I helped!
Topic archived. No new replies allowed.