GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 07-Sep-2007, 12:47
veronicak5678 veronicak5678 is offline
New Member
 
Join Date: Sep 2007
Posts: 27
veronicak5678 has a little shameless behaviour in the past

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:
#include <cstdio>
#include <iostream>
#include <cmath>
using namespace std;

int main ()
{
int speed, distance, time, a, b;
a=1;
b=a*speed;

cout << "Distance Program";                                         
cout << "\n\n\nWhat is the speed of the vehicle in mph?\n";         
cin >> speed;                                                       
cout << "\nHow many hours has it traveled?\n";       
cin >> time; 

if (time >1 && speed >0)
  
       cout << "\n\nHour                  Distance Traveled"; 
       cout << "\n--------------------------------------\n";
        
        while (a<= time)
                {
                  a++;
                  cout << "\n";
                  cout << a;                    
                  printf("                      ");
                  cout << b;
                  cout << "\n";
                  
                  }      
             return 0;

else   
    
      {
       cout << "\n\nYou have entered invalid data\n\n";
       system("pause");
       }
}


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 13:26. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
  #2  
Old 07-Sep-2007, 14:54
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,310
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: expected primary-expression before 'else'


Quote:
Originally Posted by veronicak5678
I keep getting the "expected primary-expression before 'else'" e!


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:
    if (time > 1 && speed > 0)  // only applies to the next statement
        cout << "\n\nHour                  Distance Traveled";

    cout << "\n--------------------------------------\n";

    while (a <= time) 
    {
        a++;
        cout << "\n";
        cout << a;
        printf("                      ");
        cout << b;
        cout << "\n";
    }
    return 0;

    else
    {
        cout << "\n\nYou have entered invalid data\n\n";
        system("pause");
    }
.

Regards,

Dave
  #3  
Old 08-Sep-2007, 08:48
ktcm ktcm is offline
New Member
 
Join Date: Jul 2007
Posts: 15
ktcm is on a distinguished road

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 GIDBlogNot selected for officer school by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
" 'expected' errors " Bren Gun C++ Forum 7 26-May-2007 15:51
Converting PHP to C and I need a little help Allenport C Programming Language 4 14-Aug-2006 13:38
Help with syntax errors PeteGallo C Programming Language 7 08-Aug-2005 20:30
Can enum have same name as class? crystalattice C++ Forum 3 08-Dec-2004 16:43
Floating point exception error (Note-long post) crystalattice C++ Forum 16 11-Sep-2004 07:37

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 17:57.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.