A fine gentleman told me that goto statements were bad, but I don't see how I can not use it here:
int main()
{
using namespace std;
int x;
int y;
int z;
int a;
int b;
Calc: //How can i get back here, without using goto?
{
cout << "To begin, type a number" << endl;
cin >> x;
cout << "Excellent!" << endl;
cout << "Now you need to type the second number" << endl;
cin >> y;
cout << "Excellent!" << endl;
cout << "Now, what do you want to do with these numbers?" << endl;
cout << "Alt. 1 +" << endl;
cout << "Alt. 2 -" << endl;
cout << "Alt. 3 *" << endl;
cout << "Alt. 4 /" << endl;
cin >> a;
if (a == 1) {
z = add(x, y);
}
if (a == 2) {
z = sub(x, y);
}
if (a == 3) {
z = mul(x, y);
}
if (a == 4) {
z = dis(x, y);
}
}
cout << "The answer to your math question is ";
cout << z << endl;
cout << "Do you want to enter another question?" << endl;
cout << "Type 1 for yes" << endl;
cout << "Type 0 for no" << endl;
cin >> b;
if (b == 1) {
goto Calc;
}
cout << "Happy trails!" << endl;
return 0;
}
It is a calculator, as you can see. Also, if you want, can you suggest a better way (If it exists) to let the user choose the operation (+ - * /). Header files are under control.
I apologize for a lot of cout statements.
Here is a cleaned-up and properly formatted version using a do/while loop for structure:
using namespace std;
int main()
{
int x, y, z, a, b;
do {
cout << "To begin, type a number" << endl;
cin >> x;
cout << "Excellent!" << endl;
cout << "Now you need to type the second number" << endl;
cin >> y;
cout << "Excellent!" << endl;
cout << "Now, what do you want to do with these numbers?" << endl;
cout << "Alt. 1 +" << endl;
cout << "Alt. 2 -" << endl;
cout << "Alt. 3 *" << endl;
cout << "Alt. 4 /" << endl;
cin >> a;
if (a == 1) {
z = add(x, y);
}
else if (a == 2) {
z = sub(x, y);
}
else if (a == 3) {
z = mul(x, y);
}
else if (a == 4) {
z = dis(x, y);
}
cout << "The answer to your math question is ";
cout << z << endl;
cout << "Do you want to enter another question?" << endl;
cout << "Type 1 for yes" << endl;
cout << "Type 0 for no" << endl;
cin >> b;
} while (b != 0);
cout << "Happy trails!" << endl;
return 0;
}
Erm , use a proper looping construct, while, for etc.
the "more generally accepted" approach in this case would be a do {...} while(b==1); but the compiled results would likely be identical.
goto makes it difficult to track where execution is coming from, and where it's going.
goto encourages spagetti-code, unless you restrict heavily where it is used (e.g. you could argue that you only use it for cleanup blocks, but such an argument makes no sense in the presence of RAII).
you are using a goto to simulate a loop. Why are you not writing a loop instead?
it's obscure and thus, makes your code less available to other people.
goto makes it more difficult to track objects lifetimes.
Short answer to actual question: No, you should not use goto in this code. There is no need for it.
The use of goto should be "when it makes the code clearer or safer". The typical example of "makes the code clearer" is when there several layers of nested loops, and some particular situation requires leaving all the nesting levels, and adding a "do we want to exit the loop" makes the code more complicated. An example of "making it safer" is if a function holds a lock, opens a file or something similar, and needs to return early - but you also need to close the file or release the lock, using "goto exit_now;" is safer than trying to remember what locks, files, etc are held and then doing return;.
This:
if (a == 1) {
z = add(x, y);
}
if (a == 2) {
z = sub(x, y);
}
if (a == 3) {
z = mul(x, y);
}
if (a == 4) {
z = dis(x, y);
}
is a classic case of you should use 'switch':
switch(a)
{
case 1:
z = add(x, y);
break;
case 2:
z = sub(x, y);
break;
....
}
Makes the code clearer - there is also no confusion about whether a changes value and maybe another if statement becomes viable.
You can easily avoid 'goto' in your code. Just divide it into functions:
using namespace std;
void question () {
cout << "To begin, type a number" << endl;
cin >> x;
// put rest of the code here
}
int main () {
int ask = 1;
while ( ask == 1 ) {
question();
cout << "Do you want to enter another question?" << endl;
cout << "Type 1 for yes" << endl;
cout << "Type 0 for no" << endl;
cin >> ask;
}
return 0;
}
Edit: as noted in the comments, using do-while would be actually an better option.
goto isn't automatically bad. Unreadable code is bad. Whenever you find yourself in need of some obscure programming construct like 'goto', that usually means that your code is either poorly written, or that your program design is flawed.
The solution is almost always more functions. For example:
bool run_program();
int prompt_user_begin();
int prompt_user_again();
int prompt_operation_type();
bool prompt_continue();
int main()
{
while(run_program())
{}
cout << "Happy trails!" << endl;
return 0;
}
bool run_program()
{
int first;
int second;
int operation_type;
int result;
first = prompt_user_begin();
cout << "Excellent!" << endl;
second = prompt_user_again();
cout << "Excellent!" << endl;
operation_type = prompt_operation_type();
switch(operation_type)
{
case 1: result = add(first, second); break;
case 2: result = sub(first, second); break;
case 3: result = mul(first, second); break;
case 4: result = div(first, second); break;
}
cout << "The answer to your math question is ";
cout << result << endl;
return prompt_continue();
}
int prompt_user_begin ()
{
int x;
cout << "To begin, type a number" << endl;
cin >> x;
return x;
}
int prompt_user_again ()
{
int x;
cout << "Now you need to type the second number" << endl;
cin >> x;
return x;
}
int prompt_operation_type ()
{
int x;
cout << "Now, what do you want to do with these numbers?" << endl;
cout << "Alt. 1 +" << endl;
cout << "Alt. 2 -" << endl;
cout << "Alt. 3 *" << endl;
cout << "Alt. 4 /" << endl;
cin >> x;
return x;
}
bool prompt_continue ()
{
int x;
cout << "Do you want to enter another question?" << endl;
cout << "Type 1 for yes" << endl;
cout << "Type 0 for no" << endl;
cin >> x;
return x==1;
}
Related
I am trying to build a calculator in C++. I'm new to the program and have to do this for a school assignment, so sorry for my ignorance. English is also my second language so excuse me if I don't make much sense.
Let's say I have two integers A and B for which a user has to assign a value to either add, subtract, etc. How would I then be able add a third integer (let's say X) without all three showing up when I run the program? So instead of having to type a value for A, B, AND X, it only asks to type a value for X?
For example 4 + 5 = 9, but the calculator can also square numbers, so how do I get the option of a user just filling in 4 squared = 16, while still keeping the former code that lets me add and subtract two numbers?
Maybe seeing the code would help understand what I mean? Sorry if I'm confusing.
#include <iostream.h>
#include <conio.h>
int main ()
{
cout << "Calculator [v.1.0]" << endl;
cout << "(c) 2021 <Chayenne van der Erf>" << endl << endl;
cout << "Kies een bewerking en druk op Enter:" << endl;
cout << "1. Optellen 2. Aftrekken" << endl;
cout << "3. Vermenigvuldigen 4. Delen" <<endl;
cout << "5. Kwadraat 6. Worteltrekken" <<endl;
cout << "7. Reciproke 8. Logarithme" <<endl;
cout << "0. Exit" << endl << endl;
int Bewerking;
cout << "Bewerking: ";
cin >> Bewerking;
cout << "" << endl;
switch (Bewerking) {
case 1:
cout << "+";
break;
case 2:
cout << "-";
break;
case 3:
cout << "*";
break;
case 4:
cout << "/";
break;
default: "Invalid Number";
}
cout << "" << endl << endl;
double A, B;
cout << "Enter een waarde: ";
cin >> A;
cout << "Enter een waarde: ";
cin >> B;
int antwoord;
if (Bewerking == 1) {antwoord = A + B;}
else if (Bewerking == 2 ) {antwoord = A - B;}
else if (Bewerking == 3) {antwoord = A * B;}
else if (Bewerking == 4) {antwoord = A / B;}
cout << "" << endl;
cout << "= " << antwoord << endl;
getch();
return 0;
}
Make the variables, and the reading, conditional on the operation.
Example outline:
if (operation takes one input)
{
double x;
cin >> x;
Calculate result...
}
else if (operation takes two inputs)
{
double x, y;
cin >> x >> y;
Calculate result...
}
else if (operation takes three inputs)
{
double x, y, z;
cin >> x >> y >> z;
Calculate result...
}
Print result...
Program should begin with asking , whether to restock or continue with the current stock. The case 1 ( restock ) works perfectly , however the second case , to continue with the previous stock , returns zeros always if any of the products is zeroed.
In the textfile I have:
Milk: 10
Eggs: 2
Water: 7
Burrito: 10
Bread: 12
exit
How can i fix that ?
#include<iostream>
#include<cstdlib>
#include<fstream>
#include<string>
#include<sstream>
using namespace std;
string productName[5] = { "Milk", "Eggs", "Water", "Burrito", "Bread" };
//int productAmount[5] = { 5,12,10,4,7};
int productAmount[5];
int productPick;
int defaultPick;
int productBuy;
fstream productFile; //we create file
void loadFromFile()
{
productFile.open("productsfile.txt", ios::in);
if (productFile.good() == false)
{
cout << "Unable to load the file. Try again later." << endl;
productFile.close();
exit(0);
}
else
{
ifstream productFile("productsfile.txt");
if (productFile.is_open())
{
cout << "How may I help you?" << endl;
string line;
while (getline(productFile, line))
{
// using printf() in all tests for consistency
cout << line.c_str() << endl;
}
productFile.close();
}
}
}
void saveToFile() //this function saves in the text file the data we've globally declared. It is used only if you want to declare new variables.
{
productFile.open("productsfile.txt", ios::out);
for (int i = 0; i < 5; i++)
{
productFile << i + 1 << ". " << productName[i] << ": " << productAmount[i] << endl;
}
productFile << "6. Exit" << endl;
productFile.close();
}
void askIfDefault()
{
cout << "Do you want to come back to default stock?" << endl;
cout << "1. Yes " << "2. No " << endl;
cin >> defaultPick;
switch (defaultPick)
{
case 1:
for (int i = 0;i < 5;i++)
{
productAmount[i] = 10;
}
saveToFile();
loadFromFile();
break;
case 2:
loadFromFile();
break;
default:
cout << "I don't understand." << endl;
exit(0);
break;
}
}
void productCheck()
{
if (productAmount[productPick - 1] <= 0 || productAmount[productPick - 1] < productBuy)
{
cout << "Unfortunately we have no more " << productName[productPick - 1] << " in stock. Please choose other product from the list below: " << endl;
productAmount[productPick - 1] = 0;
}
else
{
productAmount[productPick - 1] -= productBuy;
}
}
void listOfProducts()
{
cout << "How may I help you?" << endl;
for (int i = 0; i < 5; i++)
{
cout << i + 1 << ". " << productName[i] << ": " << productAmount[i] << endl;
}
cout << "6. Exit" << endl;
}
void order()
{
cin >> productPick;
switch (productPick)
{
case 1:
cout << "How many bottles?" << endl;
cin >> productBuy;
{
productCheck();
saveToFile();
}
break;
case 2:
cout << "How many cartons?" << endl;
cin >> productBuy;
{
productCheck();
saveToFile();
}
break;
case 3:
cout << "How many multi-packs?" << endl;
cin >> productBuy;
{
productCheck();
saveToFile();
}
break;
case 4:
cout << "How many portions?" << endl;
cin >> productBuy;
{
productCheck();
saveToFile();
}
break;
case 5:
cout << "How many batches?" << endl;
cin >> productBuy;
{
productCheck();
saveToFile();
}
break;
case 6:
cout << "See you soon!" << endl;
saveToFile();
system("pause");
break;
case 666:
cout << "You cannot use the secret magic spells here." << endl;
saveToFile();
exit(0);
break;
default:
cout << "Please pick the existing product: " << endl;
saveToFile();
order();
break;
}
}
int main()
{
askIfDefault();
order();
cout << endl;
while (true && productPick != 6)
{
listOfProducts();
order();
saveToFile();
cout << endl;
}
return 0;
}
Maybe unless declaring one global fsteam productFile, try to declare the it inside each of both functions that are using it: 'loadFromFile()' and 'saveToFile()' respectively. At the beginning of them. It should be fine then.
Let me make a few additional suggestions about your code - because it's a bit difficult to follow:
Choose function names which reflect what the function does - and don't do anything in a function which exceeds what its name indicates. For example, if you wrote a function called ask_whether_to_restock() - that function should ask the question, and perhaps even get the answer , but not actually restock even if the answer was "yes" - nor write anything to files. Even reading information from a file is a bit excessive.
If you need to do more in a function than its name suggests - write another function for the extra work, and yet another function which calls each of the first two and combines what they do. For example, determine_whether_to_restock() could call read_current_stock_state() which reads from a file, and also print_stock_state() and, say, get_user_restocking_choice().
Try to avoid global variables. Prefer passing each function those variables which it needs to use (or references/pointers to them if necessary).
Don't Repeat Yourself (DRI): Instead of your repetitive switch(produtPick) statement - try writing something using the following:
cout << "How many " << unit_name_plural[productPick] << "?" << endl;
with an additional array of strings with "bottles", "cans", "portions" etc.
I am a first year cs major. Today in our lab we had to debug some code and make it work. Below is the result.
#include <iostream>
using namespace std;
int main() {
int x = 3, y;
char myanswer;
int val= 1;
int num;
y = x;
cout << "y is set to: " << y << endl;
bool again = true;
int ans;
while (again) {
cout << "Please input a number: ";
cin >> y;
if (x > y)
cout << "X is greater than Y\n";
else {
cout << "X is less than Y" << endl;
cout << "would you like to input another number?" << endl;
cin >> ans;
if (ans != 1)
break;
}
cout << "would you like to input another number ?" << endl;
cin >> ans;
if (ans != 1)
again = false;
}
for (x = 0; x < 10; x++)
cout << x << endl;
cout << "What number would you like to find the factorial for? " << endl;
cin >> num;
cout << num;
for (int x = num; x > 0; x--) {
val *= x;
}
cout << "Are you enjoying cs161? (y or n) " << endl;
cin >> myanswer;
if (myanswer == 'y')
cout << "Yay!" << endl;
else
cout << "I hope you will soon!" << endl;
return 0;
}
After the cout regarding factorials, the cin's don't work and the user ceases to be able to enter input. So far my lab ta's and friends haven't been able to find the issue. The code has been compiled and exected on both my school's engineering servers and my local computer. On both the error persists.
almost certainly this caused an overflow
for (int x = num; x > 0; x--) {
val *= x;
}
what did you enter for num?
When you have a statement as:
cout << "would you like to input another number?" << endl;
The first instinct for the user would be to type y or n as an answer. You can help the user by providing a hint.
cout << "would you like to input another number (1 for yes, 0 for no)?" << endl;
If you do that, it would be better to be consistent throughout your program. The next prompt that seeks a y/n response must use the same mechanism.
cout << "Are you enjoying cs161? (1 for yes, 0 for no) " << endl;
Of course, always validate input operations before proceeding to use the data.
if ( !(cin >> ans) )
{
// Input failed. Add code to deal with the error.
}
I'm studying functions in c++ form a book called "jumping to c++" and there are a problem exercise that is create a calculator and I need make the arithmetic operation in separate functions, sound easy and I think I did it 90% good, the program gives me the correct answer but with some random numbers.
the code is:
#include <iostream>
using namespace std;
int a, b;
int sum()
{
return a + b;
}
int subs()
{
return a - b;
}
int div()
{
return a / b;
}
int mult()
{
return a * b;
}
int ask()
{
cout << "Give me the first number: ";
cin >> a;
cout << "\nGive me the second number: ";
cin >> b;
}
int main()
{
int opcion;
cout << "1. Sum \n2. Substraction \n3. Division \n4. Multiplication \n\nChoose one option from above: \n\n";
cin >> opcion;
if(opcion == 1)
{
cout << ask();
cout << "The result is: " <<sum() <<"\n\n";
} else if (opcion == 2)
{
cout << ask();
cout << "The result is: " << subs() <<"\n\n";
}else if (opcion == 3)
{
cout <<ask();
cout << "The result is: " << div() <<"\n\n";
}else if(opcion == 4)
{
cout << ask();
cout << "The result is: " << mult() <<"\n\n";
}else
{
cout << "Error.\n\n";
}
system("pause");
}
and this is the "error/bug/whatever"
1. Sum
2. Substraction
3. Division
4. Multiplication
Choose one option from above:
4
Give me the first number: 5
Give me the second number: 5
1878005856The result is: 25
Press any key to continue . . .
notice the error before of "The result is:"
appreciate any help, thanks
ask() does not return anything so it should be a void. Also, you do not need to do cout << ask(); since ask() already does the printing inside of it and it is a void (now) so it can't be printed.
Here is the code with the modifications, see comments with **** in front for changes:
#include <iostream>
using namespace std;
int a, b;
int sum() {
return a + b;
}
int subs() {
return a - b;
}
int div() {
return a / b;
}
int mult() {
return a * b;
}
void ask() { // **** Changed to void here
cout << "Give me the first number: ";
cin >> a;
cout << "\nGive me the second number: ";
cin >> b;
}
int main() {
int opcion;
cout << "1. Sum \n2. Substraction \n3. Division \n4. Multiplication \n\nChoose one option from above: \n\n";
cin >> opcion;
if (opcion == 1) {
ask(); // **** Removed cout <<
cout << "The result is: " << sum() << "\n\n";
} else if (opcion == 2) {
ask(); // **** Removed cout <<
cout << "The result is: " << subs() << "\n\n";
} else if (opcion == 3) {
ask(); // **** Removed cout <<
cout << "The result is: " << div() << "\n\n";
} else if (opcion == 4) {
ask(); // **** Removed cout <<
cout << "The result is: " << mult() << "\n\n";
} else {
cout << "Error.\n\n";
}
system("pause");
}
You can try it here
The random number was caused by you doing cout << ask(); even though you had not returned anything.
As aschepler pointed out "make sure you enable and read compiler warnings - there should be one saying that ask() doesn't return anything although declared to return an int."
The problem is your int ask() function.
It must return int value which you are writing to console with cout << ask();
The answer above won't work because you cannot write void to cout.
Since you do not return a value then a random number retruned. My compiler marks that as an error.
Replace type of ask function:
void ask()
{
cout << "Give me the first number: ";
cin >> a;
cout << "\nGive me the second number: ";
cin >> b;
}
Then replace cout << ask(); in every if statement with just ask();
Like this:
if (opcion == 1)
{
ask();
cout << "The result is: " << sum() << "\n\n";
}
else if (opcion == 2)
{
ask();
cout << "The result is: " << subs() << "\n\n";
}
else if (opcion == 3) ...
Consider checking if b==0 in case of devision operation. Or your program will crash if u try to devide by zero.
this function is returning a random integer. convert it to void
int ask()
{
cout << "Give me the first number: ";
cin >> a;
cout << "\nGive me the second number: ";
cin >> b;
}
new
void ask()
{
cout << "Give me the first number: ";
cin >> a;
cout << "\nGive me the second number: ";
cin >> b;
}
How can I get the user to input text instead of numbers in this program.
haw can i get the cin statement to accept text? Do i have to use char?
int main()
{
using namespace std;
int x = 5;
int y = 8;
int z;
cout << "X=" << x << endl;
cout << "Y=" << y << endl;
cout << "Do you want to add these numbers?" << endl;
int a;
cin >> a;
if (a == 1) {
z = add(x, y);
}
if (a == 0) {
cout << "Okay" << endl;
return 0;
}
cout << x << "+" << y << "=" << z << endl;
return 0;
}
---EDIT---
Why won't this work?
int main()
{
using namespace std;
int x = 5;
int y = 8;
int z;
cout << "X = " << x << " Y = " << y << endl;
string text;
cout << "Do you want to add these numbers together?" << endl;
cin >> text;
switch (text) {
case yes:
z = add(x, y);
break;
case no: cout << "Okay" << endl;
default:cout << "Please enter yes or no in lower case letters" << endl;
break;
}
return 0;
}
Thank you everybody!
If you're interested, you can check out the game I made here.
http://pastebin.com/pmCEJU8E
You are helping a young programmer accomplish his dreams.
You can use std::string for that purpose. Remeber that cin reads your text until white space. If you want to read whole line use getline function from the same library.
Since you are concerned with only 1 character response from the user, as in Do you want to add these numbers? might be concatenated by a (Y/N), you should (in my opinion) use getchar() function if you have intentions to read only 1 character. This is how I would do for an error prone 1 character input handling:
bool terminate = false;
char choice;
while(terminate == false){
cout << "X=" << x << endl;
cout << "Y=" << y << endl;
cout << "Do you want to add these numbers?" << endl;
fflush(stdin);
choice = getchar();
switch(choice){
case 'Y':
case 'y':
//do stuff
terminate = true;
break;
case 'N':
case 'n':
//do stuff
terminate = true;
break;
default:
cout << "Wrong input!" << endl;
break;
}
}
As a reply to your edit
That doesn't work because you cannot pass std::string as an argument to the switch. As I told you, you should read just one character for that purpose. If you insist on using strings, do not use switch, rather go for if else blocks by using the string comparator ==.
cin >> text;
if(text == "yes"){
z = add(x, y);
}
else if(text == "no")
{
cout << "Okay" << endl;
}
else
{
cout << "Please enter yes or no in lower case letters" << endl;
}
You can use std::string.
std::string str;
std::cin>>str; //read a string
// to read a whole line
std::getline(stdin, str);