COMPILING ERRORS

closed account (91AfSL3A)
Tried doing some research on my own but I just keep changing the error codes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
int main ()
{

   string time1; // first time entry
   string time2; // second time entry
   string repeat = "y";
   while ("y" == repeat){

   cout << "Enter two times and I will print which comes first.\n\n";

   cout << "Enter the first time in military format (like, 1930): ";
   cin >> time1;
   cout << "Enter the second time: ";
   cin >> time2;

   if (time1 < time2){
      cout << time1 << time2
      } else if { (time1 > time2)
      cout << time2 << " " << time1
      } else  {(time1 == time2)
      cout << time1 << "==" << time2 << "\n\n"
      }


   cout << "Run again? (y/n)";
   cin >> repeat;
   }

    return 0;
}



ERROR CODES
:34: error: expected `;' before '}' token
:34: error: expected `(' before '{' token
:36: error: expected primary-expression before "else"
:36: error: expected `;' before "else"

On lines 17, 19, and 21 you are missing a semicolon at the end of the line.
closed account (91AfSL3A)
Thanks for the response, ya I just threw those back in to check but I still get the same errors
closed account (3qX21hU5)
1) Look at this } else if { (time1 > time2 your condition is inside your bracket. You have this a number of times through line 16 and 22 so go in and make sure your conditions are not inside your opening brackets.


2) You are missing semicolons like Zhuge said.


3) Delete your condition on the else statement on line 20. Else statement don't have a condition to them they are just saying if any of the other if statements don't meet their conditions run this.

Other then that you program looks good, some minor suggestions would be to use endl; instead of \n to create a newline since endl; also flushes the buffer and can save you from tracking down them pesky buffer errors.

Another suggestion is to look at how you format your code, make sure it is readable to other people like this part
1
2
3
4
5
6
7
   if (time1 < time2){
      cout << time1 << time2
      } else if { (time1 > time2)
      cout << time2 << " " << time1
      } else  {(time1 == time2)
      cout << time1 << "==" << time2 << "\n\n"
      }

can be kind of hard to figure out what statement are in what conditions for some people. I know that everyone has their own style and that is fine but make sure it is readable to someone that has never seen your code before.

Another suggestion which is a minor one is this. When dealing with input from the user never expect them to type it in exactly how you do it. For example

while ("y" == repeat)

If someone typed "Y" they would expect to be able to run the program again, but it wouldn't because "Y" and "y" are not the same. So instead of what you had maybe do something like this while (repeat == "Y" || repeat == "y"). The || is basically saying this: While repeat is equal to "Y" OR repeat == "y" run the loop. This way you take care of both inputs.
Last edited on
Also on 18 you have a { before your condition, it needs to be after like the if on line 16. On line 20 you have a condition on a else, which makes no sense. An else is triggered when other conditions aren't met, so you don't need to specify one.
closed account (91AfSL3A)
thank you that was some much appreciated feedback cleared up some confusion programs now functioning properly.

just for future reference when declaring my string do I need to have it match my while statement example:

1
2
string repeat = ("Y" || "y")
while (repeat == "Y" || repeat == "y")
- you can't do

1
2
3

string repeat = ("Y" || "y")


- however,you can do what's on line 2
closed account (3qX21hU5)
No you don't i'll explain it a little more in depth.

Lets look at how the flow of the program works. Lets look at this.

1
2
3
4
5
6
7
8
string repeat = "y";
while (repeat == "Y" || repeat == "y")
{
    // Do some other calculations.

    // Ask for input.
    cin >> repeat;
}


Ok so since repeat = "y" when we hit the while (repeat == "Y" || repeat == "y") it is just saying this: "If the value in repeat is equal to "Y" OR "y" we enter the loop and continue running the loop until it does not equal "Y" OR "y". So since repeat is "y" (repeat = "y") it enters the loop.

The only reason we added the extra "Y" on there is so when we hit tthe cin >> repeat statement at the end of the program that asks the user if they want to do it again by typing (y/n) it won't matter if the user enters "Y" OR "y" since we included both in the while condition.

Not sure if that was clear or not ;p getting a bit late here and my thoughts are kind of jumbled.
Last edited on
closed account (91AfSL3A)
No that is great info thanks for taking some time to clear all this up for me.
Topic archived. No new replies allowed.