2013-06-13 48 views
0

我正在创建一个程序,记录一个数字在现有文件中的频率。我的向量名称是测试,那么为什么它说“测试”没有定义?向量和频率搜索

这可能是一些小事情我在想念....

#include <ostream> 
#include <iostream> 
#include <vector> 
#include <fstream> 
#include <string> 
using namespace std; 


int main() 
{ 
string fileName; 
int aTest; 

cout << "Enter a File Name:"; 
cin >>fileName; 

ifstream inFile (fileName.c_str()); 
if (! inFile) 
{ 
cout << "!!Error in opening file 'test.dat'"<< endl; 
    } 

while(inFile >> aTest) 

    vector <int> test (101, 0) 

    test[aTest]++; 

system("pause"); 
return 0; 
} 

回答

1

你应该定义你的载体while循环之外,你应该加适量{},让您的逻辑是正确的。

尝试:

vector <int> test(101, 0); //^missing semicolon 
while(inFile >> aTest) { 
    test[aTest]++; 
    } 

同时,不要使用using namespace std,这被认为是不好的做法。

除了:

#include <ostream> //^^remove one of them, don't include unnecessary headers 
    #include <iostream> 
+0

就是这样!我会给你一个复选标记 –

+0

会做,谢谢你的信息。 –

+0

@SabrinaFolzeratti不客气。 – taocp

0

你有一个语法错误和test应环路之外定义。

vector<int> test(101, 0); // Removed whitespace and added semi-colon. 
    while(inFile >> aTest) { // Use braces for a new block. 
    test[aTest]++; 
    }