Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,113 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,066 people online right now. Registration is fast and FREE... Join Now!




Problem in if else statements (Expected Primary Expresion)

 
Reply to this topicStart new topic

Problem in if else statements (Expected Primary Expresion)

ryan2506
post 7 Oct, 2008 - 08:50 AM
Post #1


New D.I.C Head

*
Joined: 7 Oct, 2008
Posts: 3

CODE

#include <iostream>   //cin, cout
#include <iomanip>    //setw, setprecision, fixed
#include <cmath>      //pow
#include <fstream>    //ofstream
#include <string>     //assign strings

using namespace std;  //using the standard names

//function prototype

void InformationScreen();

int main()

{
  //variable declarations
  int           reading1,      //pollen level 1 of city 1
                reading2,      //pollen level 2 of city 1
                reading3,      //pollen level 3 of city 1
                reading4,      //pollen level 1 of city 2
                reading5,      //pollen level 2 of city 2
                reading6;      //pollen level 3 of city 2
  
  float     pollenlevel1,      //average of city 1's pollen level
            pollenlevel2;      //average of city 2's pollen level
  
  char      city1[75],         //name of city 1
            city2[75];         //name of city 2
  
  string    warnmsg1,          //stores city1 warning message
            warnmsg2;          //stores city2 warning message
          

  
  ofstream outputfile;
  outputfile.open("lab5report.txt"); //Declare an output file, named lab4report.txt
  InformationScreen();     //this causes the information screen to be displayed

  //user input of first city name and pollen levels
  cout <<"Enter the name of the city the pollen samples were taken from: " <<endl;
   cin.getline(city1,75); //storeing the data inputed into city1
  cout <<"Enter the first reading: " <<endl;
   cin >>reading1;
  cout <<"Enter the second reading: "<<endl;
   cin >>reading2;
  cout <<"Enter the third reading: "<<endl;
   cin >>reading3;
  
  //calculate Average pollen level of city 1
  pollenlevel1 = (reading1 + reading2 + reading3) / 3.0;
  
  //user input of second city and pollen levels
  cout <<"Enter the name of the next city pollen sample were taken from: "<<endl;
   cin.ignore();
   cin.getline(city2,75); //storeing the data inputed into city2
  cout <<"Enter the first reading: " <<endl;
   cin >>reading4;
  cout <<"Enter the second reading: "<<endl;
   cin >>reading5;
  cout <<"Enter the third reading: "<<endl;
   cin >>reading6;
  
  //calculate Average pollen level of city 2
  pollenlevel2 = (reading4 + reading5 + reading6) / 3.0;
  
   if (pollenlevel1 > 0 || pollenlevel1 < 10)
  {
      cout << pollenlevel1 << " is an invalid score.\n";
      cout << "Run the program again and make sure the pollen levels for\n";
      cout << "city 1 are integer values from 0 to 10.\n";
  }
   else
  {
   if (pollenlevel1 <= 0 && pollenlevel1 >= 2.5)
      warnmsg1 = "good";
   else if (pollenlevel1 < 2.5 && >= 5.0)
     warnmsg1 = "caution";
   else if (pollenlevel1 < 5.0 && >= 7.5)
     warnmsg1 = "warning flag";
   else if (pollenlevel1 < 7.5 && >= 10)
     warnmsg1 = "pollen action day";
  }
   if (pollenlevel2 > 0 || pollenlevel2 < 10)
  {
      cout << pollenlevel2 << " is an invalid score.\n";
      cout << "Run the program again and make sure the pollen levels for\n";
      cout << "city 2 are integer values from 0 to 10.\n";
  }
   else
  {  
   if (pollenlevel2 <= 0 && pollenlevel2 >= 2.5)
     warnmsg2 = "good";
   else if (pollenlevel2 < 2.5 && >= 5.0)
     warnmsg2 = "caution";
   else if (pollenlevel2 < 5.0 && >= 7.5)
     warnmsg2 = "warning flag";
   else if (pollenlevel2 < 7.5 && >= 10)
     warnmsg2 = "pollen action day";
  }

  //output heading to file lab4report.txt
  outputfile <<"\tCity"
             <<setw(12)<<"ST #1"
             <<setw(7) <<"ST #2"
             <<setw(7) <<"ST #3"
             <<setw(14)<<"POLLEN LEVEL"
             <<setw(9) <<"MESSAGE\n"<<endl;
            
  //output data to file lab4report.txt
  cout << setprecision( 3 ) << fixed << showpoint;
  
  //output city 1 and data accordingly
  outputfile <<left<< setprecision(1)<<fixed<<showpoint
              <<setw(15)<<city1
               <<right<<setw(7)<<reading1
               <<setw(7)<<reading2
               <<setw(7)<<reading3
               <<setw(12)<<pollenlevel1
               <<left<<setw(19)<<warnmsg1<<endl;

  //output city 2 and data accordingly
  outputfile <<left<< setprecision(1)<<fixed<<showpoint
             <<setw(15)<<city2
             <<right<<setw(7)<<reading4
             <<setw(7)<<reading5
             <<setw(7)<<reading6
             <<setw(12)<<pollenlevel2
             <<left<<setw(19)<<warnmsg2<<endl;
                  
  
   return 0;
}


I'm sure I have placement errors among other things but I keep getting (Expected Primary Expression before '>=' token) for everyone of my if else statements.
Also If there is a way to make all the info for city 1 and city 2 in 1 bit If Statement then output at the end. I would like to know how.
Another not We have not discussed Loops or anything farther then what i have in this code in class and I will get in trouble for using it.
So please use the basic methods smile.gif
User is offlineProfile CardPM

Go to the top of the page

JackOfAllTrades
post 7 Oct, 2008 - 08:57 AM
Post #2


D.I.C Addict

Group Icon
Joined: 23 Aug, 2008
Posts: 500



Thanked 44 times

Dream Kudos: 25
My Contributions


You're missing the pollenlevel1 in the second conditional of those statements:
cpp
else if (pollenlevel1 < 2.5 && pollenlevel1 >= 5.0)
User is online!Profile CardPM

Go to the top of the page

ryan2506
post 7 Oct, 2008 - 09:03 AM
Post #3


New D.I.C Head

*
Joined: 7 Oct, 2008
Posts: 3

Man I can not believe I did not see that. I guess thats how it goes. I appreciate your help maybe 1 day I will be able to return the favor for someone else smile.gif
CODE

#include <iostream>   //cin, cout
#include <iomanip>    //setw, setprecision, fixed
#include <cmath>      //pow
#include <fstream>    //ofstream
#include <string>     //assign strings

using namespace std;  //using the standard names

//function prototype


int main()

{
  //variable declarations
  int           reading1,      //pollen level 1 of city 1
                reading2,      //pollen level 2 of city 1
                reading3,      //pollen level 3 of city 1
                reading4,      //pollen level 1 of city 2
                reading5,      //pollen level 2 of city 2
                reading6;      //pollen level 3 of city 2
  
  float     pollenlevel1,      //average of city 1's pollen level
            pollenlevel2;      //average of city 2's pollen level
  
  char      city1[75],         //name of city 1
            city2[75];         //name of city 2
  
  string    warnmsg1,          //stores city1 warning message
            warnmsg2;          //stores city2 warning message
          

  
  ofstream outputfile;
  outputfile.open("lab5report.txt"); //Declare an output file, named lab4report.txt
  InformationScreen();     //this causes the information screen to be displayed

  //user input of first city name and pollen levels
  cout <<"Enter the name of the city the pollen samples were taken from: " <<endl;
   cin.getline(city1,75); //storeing the data inputed into city1
  cout <<"Enter the first reading: " <<endl;
   cin >>reading1;
  cout <<"Enter the second reading: "<<endl;
   cin >>reading2;
  cout <<"Enter the third reading: "<<endl;
   cin >>reading3;
  
  //calculate Average pollen level of city 1
  pollenlevel1 = (reading1 + reading2 + reading3) / 3.0;
  
  //user input of second city and pollen levels
  cout <<"Enter the name of the next city pollen sample were taken from: "<<endl;
   cin.ignore();
   cin.getline(city2,75); //storeing the data inputed into city2
  cout <<"Enter the first reading: " <<endl;
   cin >>reading4;
  cout <<"Enter the second reading: "<<endl;
   cin >>reading5;
  cout <<"Enter the third reading: "<<endl;
   cin >>reading6;
  
  //calculate Average pollen level of city 2
  pollenlevel2 = (reading4 + reading5 + reading6) / 3.0;
  
   if (pollenlevel1 > 0 || pollenlevel1 < 10)
  {
      cout << pollenlevel1 << " is an invalid score.\n";
      cout << "Run the program again and make sure the pollen levels for\n";
      cout << "city 1 are integer values from 0 to 10.\n";
  }
   else
  {
   if (pollenlevel1 <= 0 && pollenlevel1 >= 2.5)
      warnmsg1 = "good";
   else if (pollenlevel1 < 2.5 && pollenlevel1 >= 5.0)
     warnmsg1 = "caution";
   else if (pollenlevel1 < 5.0 && pollenlevel1 >= 7.5)
     warnmsg1 = "warning flag";
   else if (pollenlevel1 < 7.5 && pollenlevel1 >= 10)
     warnmsg1 = "pollen action day";
  }
   if (pollenlevel2 > 0 || pollenlevel2 < 10)
  {
      cout << pollenlevel2 << " is an invalid score.\n";
      cout << "Run the program again and make sure the pollen levels for\n";
      cout << "city 2 are integer values from 0 to 10.\n";
  }
   else
  {  
   if (pollenlevel2 <= 0 && pollenlevel2 >= 2.5)
     warnmsg2 = "good";
   else if (pollenlevel2 < 2.5 && pollenlevel2 >= 5.0)
     warnmsg2 = "caution";
   else if (pollenlevel2 < 5.0 && pollenlevel2 >= 7.5)
     warnmsg2 = "warning flag";
   else if (pollenlevel2 < 7.5 && pollenlevel2 >= 10)
     warnmsg2 = "pollen action day";
  }

  //output heading to file lab4report.txt
  outputfile <<"\tCity"
             <<setw(12)<<"ST #1"
             <<setw(7) <<"ST #2"
             <<setw(7) <<"ST #3"
             <<setw(14)<<"POLLEN LEVEL"
             <<setw(9) <<"MESSAGE\n"<<endl;
            
  //output data to file lab4report.txt
  cout << setprecision( 3 ) << fixed << showpoint;
  
  //output city 1 and data accordingly
  outputfile <<left<< setprecision(1)<<fixed<<showpoint
              <<setw(15)<<city1
               <<right<<setw(7)<<reading1
               <<setw(7)<<reading2
               <<setw(7)<<reading3
               <<setw(12)<<pollenlevel1
               <<left<<setw(19)<<warnmsg1<<endl;

  //output city 2 and data accordingly
  outputfile <<left<< setprecision(1)<<fixed<<showpoint
             <<setw(15)<<city2
             <<right<<setw(7)<<reading4
             <<setw(7)<<reading5
             <<setw(7)<<reading6
             <<setw(12)<<pollenlevel2
             <<left<<setw(19)<<warnmsg2<<endl;
                  
  
   return 0;
}



This post has been edited by ryan2506: 7 Oct, 2008 - 09:14 AM
User is offlineProfile CardPM

Go to the top of the page

ryan2506
post 7 Oct, 2008 - 09:12 AM
Post #4


New D.I.C Head

*
Joined: 7 Oct, 2008
Posts: 3

Hmmm now I'm having a problem where it outputs the heading and all the data and average but not the warning message. Like its not being stored or I am calling it to output in the wrong place.
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/21/08 10:07AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month