views:

134

answers:

6

I have written out the program, however once I tried compileing it a get syntax errors and cannot fine where the syntax errors are. Would you know why my program won't compile? It should calculate number of second sound travels in difference gases, information given by user.

include <iostream>
#include <fstream>
using namespace std;

   int main()
         {
          int choice, gascount=0,i,sec;
          string gas[10],type;
          double speed[10],speedd;
          ifstream input;
   input.open("input.txt"); 
   if(input.fail())            
          cout<<"file did not open please check it\n";
          cin >> gas[gascount++];
      while(input)
          {
              input>>speed[gascount];
              input>>gas[++gascount];
          }
           while(choice!=5)  
               {cout<<"Choose gas would you like to use\n";
                cout<<"1  CarbonDioxide\n";
                cout<<"2  Air\n";
                cout<<"3  Helium\n";
                cout<<"4  Hydrogen\n";
                cout<<"5  Exit\n";  //5th cout for exiting program
                cin >>choice;

     switch(choice)    //use swich for user selection of gases 
        {case 1:    type="CarbonDioxide";
                    break;
         case 2:    type="Air";
                    break;
         case 3:    type="Helium";
                    break;
         case 4:    type="Hydrogen";
                    break;
         case 5:    system("exit");
         default:   printf("Illegal input: Try Again\n");
         }
          i=0;
          for(i=0;i<gascount;i++)            //loop for number of seconds
             if(type.compare(gas[i])==0)   //speed travel in gases
                {speedd=speed[i];
                 i=gascount+1;
                 }
      cout<<"You chose "<<type<<endl;
      cout<<"how many seconds did the sound travel? ";
      cin>>sec;
      while(sec<0||sec>30)
         {cout<<"must me between 0 and 30\n";
          cout<<"how many seconds did the sound travel? ";
          cin>>sec;
         }
          cout<<"The speed of sound through "<<type<<" is "<<speedd*sec*10.<<endl;
    }
    input.close();
    system("pause");

 return 0;

}
+1  A: 

I could see 2 problems:

In the first line # is missing before the include ( could be a typo)

You are using the system function but not including stdlib, you need a

#include <cstdlib>
codaddict
+5  A: 

Your complier has error messages. You need to look at those messages and fix them one at a time If you can't figure one our repost it.

rerun
While generally a good point, in this case quite a few compilers are likely to emit error messages that are quite unhelpful.
Jerry Coffin
Part of the process of being a good c/c++ coder is learning what the complier is trying to tell you.
rerun
A: 

Once you've fixed the problems @codaddict pointed out, you probably also want to #include <string>, since you're using std::string.

Although it's not directly related, you should also work on indenting your code a bit more meaningfully.

It looks like you're also using choice before you initialize it.

Jerry Coffin
A: 

Yes, you are probably missing

#include <cstdlib> // system() defined here
#include <string>  // std::string here
  • Don't miss to include required headers.
  • Work on your indentation.
  • There is no problem with choice but initializing it at the point of definition would be good style.
  • Take into account warnings too

    prog.cpp:40: warning: ignoring return value of ‘int system(const char*)’, declared with attribute warn_unused_result

    prog.cpp:60: warning: ignoring return value of ‘int system(const char*)’, declared with attribute warn_unused_result

    prog.cpp:11: warning: ‘speedd’ may be used uninitialized in this function

Keynslug
A: 

#include <stdlib> and #include <stdio> may help...

KA_lin
ITYM `<cstdlib>` and `<cstdio>` ?
Paul R
A: 

Just add #include < string > and your program will compile perfectly :)

Kiril Kirov
you can't compile your program, because the `operator>>` is defined in `string` header.
Kiril Kirov