What have I done wrong?

Dev c++ will not compile this. In my first set of if else statements, it says it expects a semi colon before the string literals.






#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

int main()

{

double time1, time2, time3;
string name1, name2, name3;

cout<<"This program will demonstrate what racers won first, second, and third place\n";
cout<<"in said race when the racers and their times are entered.\n";
cout<<"Please use the following example to know how to input your information:\n";
cout<<"Harry 5.5 John 6.2 Jim 6.5. Please note that the numbers entered should be\n";
cout<<" in decimal form of time. For example: a time of 6:30 would be entered as 6.5.\n";
cout<<"Please input the information now: ";
cin>> name1 >> time1 >> name2 >> time2 >> name3 >> time3;
cout<< setprecision (1) <<fixed;
while (time1<=0 || time2<=0 || time3<=0)
{
cout<<"You must enter a number greater than 0 for the each of the racers' times.\n";
cout<<"Please re enter the numbers in the same order previously entered (NO NAMES): \n";
cin>> time1 >> time2 >> time3;
}
if (time1==time2 || time1==time3 || time2==time3)
if (time1==time2)
{if (time1>time3)
{cout<<"Runners " <<name1 <<" and " <<name2 <<" tied for first place with a time of " <<time1 <<"!!!"<<endl;
cout<<"While " <<name3 <<" came in third place with a time of " <<time3 <<endl;}


else
{cout<< name3 <<" came in first place with a time of " <<time3 "!!!\n";
cout<<"While " <<name2 <<" and " <<name1 <<" tied for second place with a time of "<<time1 ".n";}
}
else
if (time1==time3)
if (time1>time2)
{cout<<"Runners " <<name1 <<" and " <<name3 <<" tied for first place with a time of " <<time1 <<"!!!"<<endl;
cout<<"While " <<name2 <<" came in third place with a time of " <<time2 <<endl;
}
else
{cout<<name2 <<" came in first place with a time of " <<time2 "!!!\n";
cout<<"While " <<name1 <<" and " <<name3 <<" tied for second place with a time of "<<time1 ".n";
}
else
if (time2==time3)
if (time2>time3)
{cout<<"Runners " <<name2 <<" and " <<name3 <<" tied for first place with a time of " <<time2 <<"!!!"<<endl;
cout<<"While " <<name1 <<" came in third place with a time of " <<time1 <<endl;
}
else
{cout<<name1 <<" came in first place with a time of " <<time1 "!!!\n";
cout<<"While " <<name2 <<" and " <<name3 <<" tied for second place with a time of "<<time2 ".n";
}
else





if (time1>time2 && time1>time3)
{
if (time2>time3)
{ cout<<"The runners came in place as follows:\n";
cout<<"1st place was " <<name1 <<"With a time of " <<time1 <<"!!!" <<endl;
cout<<"2nd place was " <<name2 <<"With a time of " <<time2 <<"!!!" <<endl;
cout<<"3rd place was " <<name3 <<"With a time of " <<time3 <<"!!!" <<endl;
}
else
{ cout<<"The runners came in place as follows:\n";
cout<<"1st place was " <<name1 <<" with a time of " <<time1 <<"!!!" <<endl;
cout<<"2nd place was " <<name3 <<" with a time of " <<time3 <<"!!!" <<endl;
cout<<"3rd place was " <<name2 <<" with a time of " <<time2 <<"!!!" <<endl;
}
}
else
if (time2>time1 && time2> time3)
if(time1>time3)

{ cout<<"The runners came in place as follows:\n";
cout<<"1st place was " <<name2 <<"With a time of " <<time2 <<"!!!" <<endl;
cout<<"2nd place was " <<name1 <<"With a time of " <<time1 <<"!!!" <<endl;
cout<<"3rd place was " <<name3 <<"With a time of " <<time3 <<"!!!" <<endl;
}
else
{cout<<"1st place was " <<name2 <<"With a time of " <<time2 <<"!!!" <<endl;
cout<<"2nd place was " <<name3 <<"With a time of " <<time3 <<"!!!" <<endl;
cout<<"3rd place was " <<name1 <<"With a time of " <<time1 <<"!!!" <<endl;
}

else
if (time2>time1)
{cout<<"1st place was " <<name3 <<"With a time of " <<time3 <<"!!!" <<endl;
cout<<"2nd place was " <<name2 <<"With a time of " <<time2 <<"!!!" <<endl;
cout<<"3rd place was " <<name1 <<"With a time of " <<time1 <<"!!!" <<endl;
}
else
{cout<<"1st place was " <<name3 <<"With a time of " <<time3 <<"!!!" <<endl;
cout<<"2nd place was " <<name1 <<"With a time of " <<time1 <<"!!!" <<endl;
cout<<"3rd place was " <<name2 <<"With a time of " <<time2 <<"!!!" <<endl;
}

cout<<endl;
system("pause");
return 0;
}










If you could use [ Code] [ /Code] tags around your code and make sure it is all indented correctly it would be much easier to see the issue.
Please put your code in code tags, there the <> symbols. Second Devc++ is a decrepit compiler, I suggest Code::Blocks as its a crapload better.

http://www.codeblocks.org/downloads

Also your problem lies here

{cout<<name2 <<" came in first place with a time of " <<time2 "!!!\n";

you forgot the << between time2 and "!!!\n";

Also your using system wich is an OS specific command to windows, either use cin.get(); which is better than the latter or put #include <windows.h> in your file if you want to use system pause
Last edited on
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

int main()

{

double time1, time2, time3;
string name1, name2, name3;

cout<<"This program will demonstrate what racers won first, second, and third place\n";
cout<<"in said race when the racers and their times are entered.\n";
cout<<"Please use the following example to know how to input your information:\n";
cout<<"Harry 5.5 John 6.2 Jim 6.5. Please note that the numbers entered should be\n";
cout<<" in decimal form of time. For example: a time of 6:30 would be entered as 6.5.\n";
cout<<"Please input the information now: ";
cin>> name1 >> time1 >> name2 >> time2 >> name3 >> time3;
cout<< setprecision (1) <<fixed;
 while (time1<=0 || time2<=0 || time3<=0)
   { 
     cout<<"You must enter a number greater than 0 for the each of the racers' times.\n";
     cout<<"Please re enter the numbers in the same order previously entered (NO NAMES): \n";
     cin>> time1 >> time2 >> time3;
     }
     if (time1==time2 || time1==time3 || time2==time3)
        if (time1==time2)
            {if (time1>time3)
               {cout<<"Runners " <<name1 <<" and " <<name2 <<" tied for first place with a time of " <<time1 <<"!!!"<<endl;
                cout<<"While " <<name3 <<" came in third place with a time of " <<time3 <<endl;}
                
           
            else 
                 {cout<< name3 <<" came in first place with a time of " <<time3 "!!!\n";
                 cout<<"While " <<name2 <<" and " <<name1 <<" tied for second place with a time of "<<time1 ".n";}
                 }
        else
            if (time1==time3)
                if (time1>time2)       
                {cout<<"Runners " <<name1 <<" and " <<name3 <<" tied for first place with a time of " <<time1 <<"!!!"<<endl;
                cout<<"While " <<name2 <<" came in third place with a time of " <<time2 <<endl;
                }
                else
                {cout<<name2 <<" came in first place with a time of " <<time2 "!!!\n";
                cout<<"While " <<name1 <<" and " <<name3 <<" tied for second place with a time of "<<time1 ".n";
                }
            else
                if (time2==time3)
                   if (time2>time3)
                   {cout<<"Runners " <<name2 <<" and " <<name3 <<" tied for first place with a time of " <<time2 <<"!!!"<<endl;
                   cout<<"While " <<name1 <<" came in third place with a time of " <<time1 <<endl;
                   }
                   else
                   {cout<<name1 <<" came in first place with a time of " <<time1 "!!!\n";
                   cout<<"While " <<name2 <<" and " <<name3 <<" tied for second place with a time of "<<time2 ".n";
                   }
    else
    
    
    
    
    
     if (time1>time2 && time1>time3)
       {
        if (time2>time3)
          {  cout<<"The runners came in place as follows:\n";
             cout<<"1st place was " <<name1 <<"With a time of " <<time1 <<"!!!" <<endl;
             cout<<"2nd place was " <<name2 <<"With a time of " <<time2 <<"!!!" <<endl;
             cout<<"3rd place was " <<name3 <<"With a time of " <<time3 <<"!!!" <<endl;
             }
        else 
          {  cout<<"The runners came in place as follows:\n";
             cout<<"1st place was " <<name1 <<" with a time of " <<time1 <<"!!!" <<endl;
             cout<<"2nd place was " <<name3 <<" with a time of " <<time3 <<"!!!" <<endl;
             cout<<"3rd place was " <<name2 <<" with a time of " <<time2 <<"!!!" <<endl;           
             }
             }
     else 
             if (time2>time1 && time2> time3)
                if(time1>time3)
             
             {  cout<<"The runners came in place as follows:\n";
             cout<<"1st place was " <<name2 <<"With a time of " <<time2 <<"!!!" <<endl;
             cout<<"2nd place was " <<name1 <<"With a time of " <<time1 <<"!!!" <<endl;
             cout<<"3rd place was " <<name3 <<"With a time of " <<time3 <<"!!!" <<endl;
             }
              else 
                {cout<<"1st place was " <<name2 <<"With a time of " <<time2 <<"!!!" <<endl;
                 cout<<"2nd place was " <<name3 <<"With a time of " <<time3 <<"!!!" <<endl;
                 cout<<"3rd place was " <<name1 <<"With a time of " <<time1 <<"!!!" <<endl;
                 }
             
             else
                 if (time2>time1)
                    {cout<<"1st place was " <<name3 <<"With a time of " <<time3 <<"!!!" <<endl;
                     cout<<"2nd place was " <<name2 <<"With a time of " <<time2 <<"!!!" <<endl;
                     cout<<"3rd place was " <<name1 <<"With a time of " <<time1 <<"!!!" <<endl;
                     } 
                 else
                    {cout<<"1st place was " <<name3 <<"With a time of " <<time3 <<"!!!" <<endl;
                     cout<<"2nd place was " <<name1 <<"With a time of " <<time1 <<"!!!" <<endl;
                     cout<<"3rd place was " <<name2 <<"With a time of " <<time2 <<"!!!" <<endl;
                     }
                     
cout<<endl;
 system("pause");
return 0;
}             










I tried using code blocks at the first of this semester. My whole class had a problem with it. Our instructor said he thought it might have been an update that caused the problems. Anyway, I'm used to Dev now so I'll probably stick with it.
ah i see, well i edited my post and found your problems all you need to do is implement them.
Last edited on
Thank very much, this explains why I had so many errors. I copied and pasted that block of code for the next few. Is my code condensed well enough?
yeah for what you have its ok. But I would suggest looking into switch statements.
when I run the program now, if I give a statement to continue my loop more than once, it turns into an infinite loop :/
let me see the code please.
Last edited on
1
2
else
                 if (time2>time1)


Can I suggest that you don't do this? C++ has else if (condition){} as well as else {}

It looks like you are mixing the 2 concepts together, the proper format looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
if (condition) {
//statements
}
else if (condition) {
//statements
}
else if (condition) {
//statements
}
else {
//statements
}


It might work at the moment, but it will confuse you one day & is causing undue complication in your code, because you are effectively putting all the else if's inside the else.

You also need some functions - lines 67 - 71 would be a great example, although i would the results into an array in order, rather than a whole lot of code to retrieve the right value (40 lines of code covering 6 permutations versus 3 lines of code)

Your assignment seems to require conversion from minute and seconds into decimal minutes, but you don't do that anywhere.

Also, you shouldn't use equality operators on floats or doubles directly. It may work now, but it will cause problems later on.

Floating point numbers are stored as binary fractions, and cannot represent all real numbers exactly. Consider this:

1
2
3
4
float a = 0.1; // a == 0.09999997
float b = 10 * a; // b == 0.9999997

if (b == 1.0)  //always false 


Changing the type to double doesn't help.

To fix this, Google epsilon.

http://en.cppreference.com/w/cpp/types/numeric_limits/epsilon
http://www.cplusplus.com/reference/limits/numeric_limits/


Topic archived. No new replies allowed.