Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help With Errors...

Status
Not open for further replies.
Sep 9, 2002
28
0
0
US
I am a beginner at C++ and am currently working on my first assignment for class. I am running into some errors.
The assignment is to convert seconds of travel to hours/minutes/seconds.

My Code:

//Andrew Brudtkuhl
//ComS 207, section b1
//wed, 9am

#include (iostream.h)

void main (void)

{
int tseconds, tminutes, hours, minutes, seconds;

cout << &quot;Please enter number seconds it took you to get from New York to Los Angeles&quot;;
cin >> tseconds

tseconds/60 = tminutes
tminutes/60 = hours
(tminutes%60)60 = minutes
(tminutes%60)%60 = seconds

cout << &quot;The time it takes to get from New Your to Los Angeles is:&quot;; << hours; << &quot; &quot;; << minutes; << &quot; &quot;;
<< seconds;

return 0;

}

My Errors:

hw3.C: In function 'int main (...)':
hw3.C:15: parse error before '/'

If anyone can help it would be greatly appreciated.
 
Code:
   cin >> tseconds
Code:
;

You're missing a semi-colon...

When assigning value to a variable, it's the variable on the left and then value on the right
Code:
int x;

x = 12;
Code:
// will assign x to 12
Code:
12 = x;
Code:
// This does NOT work

So what you want to do is change
Code:
tseconds/60 = tminutes;
to
Code:
tminutest = seconds/60;
And the same for the next few lines.


And last...
Code:
  cout << &quot;The time it takes to get from New Your to Los Angeles is:&quot;; << hours; << &quot; &quot;; << minutes; << &quot; &quot;;
         << seconds;

You add many semi-colons inbetween the << operator. This is wrong. the &quot;;&quot; is used a line seperated parsing device. This will make you compiler think (in a more visible manner):
Code:
  cout << &quot;The time it takes to get from New Your to Los Angeles is:&quot;;
 << hours;
 << &quot; &quot;;
 << minutes;
 << &quot; &quot;;        
 << seconds;

And the << operator does not work that way. So what you need to do here is drop every semicolon in that line but the last.

Code:
  cout << &quot;The time it takes to get from New Your to Los Angeles is:&quot; << hours << &quot; &quot; << minutes << &quot; &quot; << seconds;
 
Oh - and btw. You probably should name your files something.cpp... Some compilers may identify the type of &quot;compilation&quot; used by the file's extention.

.c = C
.cpp = C++
 
thank you...

changed code:

//Andrew Brudtkuhl
//ComS 207, section b1
//wed, 9am

#include <iostream.h>

void main(void)

{
int tseconds, tminutes, hours, minutes, seconds;

cout << &quot;Please enter number seconds it took you to get from New York to Los Angeles&quot;;
cin >> tseconds;

tminutes = tseconds/60
hours = tminutes/60
minutes = (tminutes%60)60
seconds = (tminutes%60)%60

cout<< &quot;The time it takes to get from New Your to Los Angeles is:&quot; << hours << &quot; &quot; << minutes << &quot; &quot; << seconds;


return 0;
}

still getting errors:
hw3.C: In function 'int main (...)':
hw3.C:16: parse error before '='

 
the compiler we use at school reads .C as C++ and .c as C...
 
Ah.. okay for the extension. : ) But as for the continuing errors: You are missing your semi-colons. Those declare the end of a statement and must be there. Unlike BASIC or a few other languages, an end line does not signify the completion of a statement...

Code:
tminutes = tseconds/60
Code:
;
Code:
 //<-- that semi-colon NEEDS to be there

The lines of code afterwards,
Code:
hours = tminutes/60
minutes = (tminutes%60)60
seconds = (tminutes%60)%60
Will also need them.
 
Continuing explanation of semi-colon being the end of a statement

For example:
Code:
int x;

x
=
12
;
is equivelent to
Code:
int x; x=12;
 
Also, you're returning 0 from main, but main is declared as returning void. It should return int. (Having main return void is always wrong anyway, even if the compiler doesn't care).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top