Can anyone help me debug this C++ error "error C2143: syntax error : missing ';' before 'constant'"

2008-07-28 4:13 am
#include <iostream>
#include <fstream>
#include <cstdlib>
using std::cout;
using std::cin;
using std::endl;
using std::cerr;
using std::ios;
using std::exit;
using std::ofstream;


int main()
{
ofstream outClientFile( "clients.dat", ios::out);
if( !outClientFile )
{
cerr << "File could not be opened" << endl; // Exit program if unable to create file.
exit(1);
}
cout << "Enter the account, name, and balance." << endl;
cout << "Enter end-of-file to end input. \n? " << endl;
int account;
char name[30];
int balance;
cin >> account >> name >> balance;
outClientFile << account << ' ' << name ' ' << balance;
cout << endl;
cout << "? ";
return 0;
}

/*NOTE...The error points to this line...outClientFile << account << ' ' << name ' ' << balance;????*/

回答 (6)

2008-07-28 4:28 am
✔ 最佳答案
I'm not a C programmer but it looks like you need to use double quotes rather than single ones on the outClientFile line. Also, you need to put a << after name.
2008-07-28 6:38 am
outClientFile << account << " " << name << " " << balance;

If you want a space, you need to use double quotes, not single.
2008-07-28 4:29 am
I think you need another "<<" in between "name" and the empty character "''" (2 adjacent single quotes).
2016-05-29 1:44 pm
The for loop should have semicolon instaead of comma for(counter=1 ; counter<=years ; counter++)
2008-07-28 5:42 am
I want to second Shinkou. There is no reason you can't sen char constants rather than single char string constants for spaces but when it comes to name ' ' that is an error and should be name << ' ' <<...
2008-07-28 5:18 am
String constants must be surrounded by double quote (")


收錄日期: 2021-05-01 01:00:00
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20080727201313AAq1RWS

檢視 Wayback Machine 備份