![]() |
|
#1
|
|||
|
|||
expected primary-expression before 'else'I am tyring to write a simple program to display distance traveled. here are my instructions:
Write a program that asks the user for the speed of a vehicle (In miles per hour) and how many hours it has traveled. It should then use a loop to display the distance the vehicle has traveled for each hour of that time period. Her is an example of the output What is the speed of the vehicle in mph? 40 How many hours has it traveled? 3 Hour Distance Traveled -------------------------------------------------- 1 40 2 80 3 120 Input validation: Do not accept any negative number for speed and do not accept any value less than one for time traveled. The output should be formatted and aligned according to the above. Here is my code so far: CPP / C++ / C Code:
I keep getting the "expected primary-expression before 'else'" error and the "expected ";" before 'else'" error. Am I missing something very simple? Imay not know much about programming, but I thought this was straight-forward! Last edited by LuciWiz : 07-Sep-2007 at 14:26.
Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
|
|||
|
#2
|
|||
|
|||
Re: expected primary-expression before 'else'Quote:
So: what do you have before your "else'? The compiler ignores indentation. The "if" thingie only works on one statement; the one that follows immediately. If you need more than one statement, then put them in a block (surround the block with{} braces, as you did with the "while" thingie and the "else" thingie) I have reformatted part of your code with indentation consistent with your logic. Here is what the compiler sees: CPP / C++ / C Code:
Regards, Dave |
|
#3
|
|||
|
|||
Re: expected primary-expression before 'else'Yeah, I also think that everything between the if and the else statement should be enclosed in {} braces for them to be processed as a 'if' block. I also think that the logic for calculating distance travelled should be within the while loop, as in this case, it is only calculating the distance travelled once.
|
Recent GIDBlog
Once again, no time for hobbies by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| " 'expected' errors " | Bren Gun | C++ Forum | 7 | 26-May-2007 16:51 |
| Converting PHP to C and I need a little help | Allenport | C Programming Language | 4 | 14-Aug-2006 14:38 |
| Help with syntax errors | PeteGallo | C Programming Language | 7 | 08-Aug-2005 21:30 |
| Can enum have same name as class? | crystalattice | C++ Forum | 3 | 08-Dec-2004 17:43 |
| Floating point exception error (Note-long post) | crystalattice | C++ Forum | 16 | 11-Sep-2004 08:37 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The