我编写了这个程序,但是当我尝试编译它时,我遇到了语法错误。一世 找不到语法错误的位置。
它应该计算不同气体中的第二声音传播次数,用户给出的信息。
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;
}
答案 0 :(得分:5)
您的编译器有错误消息。您需要查看这些消息并一次修复一个消息如果您无法找到它们的重新发布。
答案 1 :(得分:2)
我可以看到2个问题:
第一行#
在include
之前丢失(可能是拼写错误)
您使用的是system
功能,但不包括stdlib
,您需要
#include <cstdlib>
答案 2 :(得分:0)
一旦你解决了@codaddict指出的问题,你可能还想#include <string>
,因为你正在使用std::string
。
虽然它没有直接关系,但您还应该更有意义地缩进代码。
在初始化之前,您似乎也在使用choice
。
答案 3 :(得分:0)
是的,你可能错过了
#include <cstdlib> // system() defined here
#include <string> // std::string here
choice
没有问题,但在定义点初始化它会很好。也要考虑警告
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
答案 4 :(得分:0)
#include <stdlib>
和#include <stdio>
可能会有所帮助......
答案 5 :(得分:0)
添加#include < string >
,您的程序将完美编译:)