Sign in to follow this  
Emotions06

Help please (coding)

Recommended Posts

Emotions06    122
Okay so I have been doing tutorials from cplusplus.com for a little while (day or two) and wanted to see if I was actually absorbing anything by putting some of the stuff (very basic stuff) together into a calculator and see if it would work. The code is very basic, but i'm still missing something. Can someone help? I get alot of errors saying: In function `int main()': expected primary-expression before "else" expected `;' before "else" And I get quite a few of those errors. The code is:
#include <iostream>         
#include <windows.h>        
#include <conio.h>          
using namespace std;        

float x, y;

int main() 
{
    int decision;
    
    
    cout << "Hello.  Welcome to the Calco-matic 2006!!!\nI'm here for all of your calculating needs.";
    Sleep (2000);
    cout << "Alright so lets get strarted.  Pick from the list what you would like for me to do.";
    cout << "1: Addition";
    cout << "2: Subtraction";
    cout << "3: Multiplication";
    cout << "4: Division";
    cout << "5: Square your number";
    cout << "6: Exit";
    cin >> decision;
    
    if (decision == 1)
       cout << "You choose addition." << " Please enter your first number.";
       cin >> x;
       cout << "Please enter the number you would like " << x << " to be added to.";
       cin >> y;
       cout << "Your answer is ";
       cout << x + y;
    else if (decision == 2)
       cout << "You choose subtraction." << " Please enter your first number.";
       cin >> x;
       cout << "Please enter the number you would like " << x << " to be subtracted by.";
       cin >> y;
       cout << "Your answer is ";
       cout << x - y;
    else if (decision == 3)
       cout << "You chose multiplication." << " Please enter your first number.";
       cin >> x;
       cout << "Please enter the number you would like " << x << " to be multiplied to.";
       cin >> y;
       cout << "Your answer is ";
       cout << x * y;
    else if (decision == 4)
       cout << "You chose division." << " Please enter your first number.";
       cin >> x;
       cout << "Please enter the number you would like " << x << " to be       divided by.";
       cin >> y;
       cout << "Your answer is ";
       cout << x / y;
    else if (decision == 5)
       cout << "You chose to square your number. Please enter the number you want squared.";
       cin >> x;
       cout << "Your answer is ";
       cout << x * x;
    else (decision == 6)
       break;
       
       
    return 0;
}
So my common sense would lead me to believe that I have left out a ';' before all of my else's, but I don't know where. Also I'm thinking this whole this is wrong. Am I actually right about it being totally wrong? What do I need to do to get this working correctly? Thanks alot. Also a sidenote. I'm just wondering if:
cout << x << " plus " << y << " is " << x + y << " .";
I had that at first and thought that might be the problem for some reason so changed that in favor of "Your answer is "; etc. Thanks in advance. Emotions

Share this post


Link to post
Share on other sites
alexmullins    148
you might want to try and put your if, else if, else statements in between code blocks " { } " like



if ( true )
{
do whatever;
}

else if ( true )
{
do whatever;
}

else
{
do whatever;
}


Share this post


Link to post
Share on other sites
Emotions06    122
I did that and all of the errors went away except for one down of the break; part of the code in the else statement. I'm not even sure if that is how I should have went about that final part. I didn't know what I should do to get it to go to the end if they hit 6. Was that the right way? Also what do I need to do to get that part of the code working? Thanks again.


else (decision == 6)
{
break;
}


That's what I have above and I get 2 error messages.

In function `int main()':
expected `;' before '{' token

Thanks again for help.

Share this post


Link to post
Share on other sites
Emotions06    122
Okay I changed it to:

else if (decision == 6)
{
break;
}


and now it says.

In function `int main()':
break statement not within loop or switch


Am I even supposed to be using break; here? Is there a better way to get it to exit if they enter 6? Thanks.

Edit: I went back and did something different and added 2 goto statements in there. I also added code so that it would ask if you wanted to perform another problem. I added a if statement was yes goto loop; (placed the loop right before I asked which they would like 1:addition, etc) and added an else if no goto done; and added done right before the return. So that does it.

But I was wondering if there was a better way to attack this project? Should I have used something like a for or while loop or something like that or not?



[Edited by - Emotions06 on November 9, 2006 9:03:24 PM]

Share this post


Link to post
Share on other sites
bschneid    126
Quote:
Original post by Emotions06
Am I even supposed to be using break; here? Is there a better way to get it to exit if they enter 6? Thanks.


I don't believe that you can use break in an if statement. If you want it to exit, then just don't give the program a condition. Think of it this way: You don't have any action defined for if the user enters anything besides 1-6. Try choosing 7. The program should exit naturally by reaching the return 0 at the end of main because there is no specified action for a 7 in the if statement. Essentially the execution skips the if statement because you never tell it what to do in the case of 7. So in order to make it exit, just don't give it any instruction for if the user inputs 6. Just know that any number besides 1-5 will still cause exit, even if it isn't 6 and that there is no practical way around this, unless you know about functions. I am going to assume you don't because of the way this program is written, so I won't confuse you with how to do it.

EDIT: Just saw your edit...I would advise you not to use goto statements because once you start writing larger programs they can be very confusing. Using a while loop can be a good approach. Create a variable, something like runProgram, and set it to true. Then, put all of your code in a while loop that runs if your variable equals true. At the end, if the user decides he wants to run the program again, make it remain true, and the loop will repeat. Otherwise, set it to false, and the loop and program will terminate.

Share this post


Link to post
Share on other sites
Emotions06    122
I went back and abandoned the if/else if statements for a switch statement and I must say it looks a whole lot cleaner this way. I still don't understand how I get it to ask if you want to do it again and do it if you say yes and quit if you say no without using a goto statement (also in the tutorial I read also said don't use goto statements so I won't).

Can someone explain how to do it? Thanks.


#include <iostream>
#include <windows.h>
#include <conio.h>
using namespace std;

int x, y, z;

int main ()
{
cout << "1:Add";
cout << "\n2:Subtract";
cout << "\n3:Exit\n";
cin >> x;

switch (x)
{
case 1:
cout << "1st number\n";
cin >> y;
cout << "\n2nd number\n";
cin >> z;
cout << "\nAnswer is: " << y + z;
break;
case 2:
cout << "1st number\n";
cin >> y;
cout << "\n2nd number\n";
cin >> z;
cout << "\nAnswer is: " << y - z;
break;
case 3:
break;
}

getch ();
return 0;
}

Share this post


Link to post
Share on other sites
Zahlman    1682
Quote:
Original post by Emotions06
I went back and abandoned the if/else if statements for a switch statement and I must say it looks a whole lot cleaner this way. I still don't understand how I get it to ask if you want to do it again and do it if you say yes and quit if you say no without using a goto statement (also in the tutorial I read also said don't use goto statements so I won't).

Can someone explain how to do it? Thanks.


By making a loop.

The most basic sort of loop is a while-loop. The syntax is as:


while (condition) {
actions;
}


It does exactly what it looks like: "while" the condition is met, the actions are performed repeatedly.

In detail: the condition is checked. If met (i.e. evaluates to true), all actions are performed. After actions are performed, the code automatically "goto"s back, and checks the condition again. This continues until the condition is not met, in which case control resumes immediately after the loop (i.e. since the condition wasn't met, the actions aren't performed, and it keeps going).

Well. You can think about it like that - in terms of how the equivalent gotos would work out. But isn't it much easier to think "as long as this is true, do that again"? :)

Now. What's your condition? Seems to be "user wants to keep going". But we don't want to check that the first time, right? We want to do the actions at least once, and check the condition at the *end* of the set of actions.

For this, we can use a do-while loop:


do {
actions;
} while (condition);


Same thing, except that the actions happen at least once (with the while loop, they might not happen, if the condition started out not being met).

In our case, it looks like:


do {
/* all the code here that handles a math problem */
} while (user_wants_to_continue());


So, how do we implement that function? Well, we need to prompt for a user's response, read the response, and interpret it as willingness-to-continue. What is the logical *type* of "willingness-to-continue"? Why, it's bool(ean), of course. Either the user wishes to continue or s/he does not.

Thus:


bool user_wants_to_continue() {
cout << "Do it again? " << endl;
// This is how we read a line of input robustly:
std::string response;
std::getline(cin, response);
// We also avoid problems with "unused" input "carrying forward" to the next
// time we read from cin.

return response == "y" || response == "Y";
// or some more complicated interpretation, to taste.
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this