2017-01-04 11 views
1

私はこのプログラムを作成しました。何らかの理由でVisual Studioがこの警告を出しています(プリプロセッサディレクティブに続く予期しないトークン - 改行が必要です)コンパイラが要求している行から1行下の内容を参照する。瞬時には、エラーCarat Errorは、適用可能なもののみを参照しているようです。one line downこのように、私は何かが、プログラムのヘッダに恐ろしく間違っていなければならないと考えていますが、unsure.Hereコードです午前:"予期せぬトークンがプリプロセッサディレクティブに続く - 改行が必要"

//1/4/2017 

#include "stdafx.h" 
#include <iostream> 
#include <vector> 
#include <string> 
#include <ctime> 
#include <cstdlib> 


using namespace std; 

void getNewItem(); 
void displayItems(); 
void displayRand(); 
vector<string> vecItems; 

int main() 
{ 
    //declaration phase 

    int intInput; 
    string strNewItem; 


    cout << "Random Item Generator" << endl << "Written by #XXXXX" << endl << "1. Add Item" << endl << "2. Display All Items " << endl << "3. Display Random Item" << endl << "4. Quit" << endl; 
    while (intInput != 4) 
    { 
     cin >> intInput; 
     switch (intInput) 
     { 
     case 1: 
      getNewItem(); 
      break; 

     case 2: 
      displayItems(); 
      break; 

     case 3: 
      displayRand(); 
      break; 
     } 
    } 
    return 0; 
} 

void getNewItem() { 
    string strNewItem; 
    cin >> strNewItem; 
    vecItems.push_back(strNewItem); 
} 

void displayItems() { 
    for (int i = 0; i < vecItems.size(); i++) { 
     cout << vecItems.at(i) << endl; 
    } 
} 

void displayRand() { 
    int intRandIndex; 
    //random number generator 
    srand((unsigned)time(0)); 
    intRandIndex = rand() % 10; 

    cout << vecItems.at(intRandIndex) << endl; 
} 

スクリーンショット1

Screenshot 1

スクリーンショット2

Screenshot 2

編集:Visual StudioのバージョンはVisual Studio 2015です。再コンパイルして実行しました新しいプロジェクトは役に立たない。

+0

ビジュアルと、このエラーを再現しません。与えられた試料Studio 2015またはVisual Studio 2010。どのバージョンのVisual Studioを使用していますか?これはあなたが与えることを意図した例ですか? –

+0

私はVisual Studio 15を使用していますが、プログラムを再コンパイルして、同じ問題を解決するために初めて作成し直しました。/ – DesPhantomes

+0

問題はプリコンパイル済みのヘッダーにある可能性があります。私はコンパイルするために '#include" stdafx.h "'という行を除外しなければなりませんでした(私はこのヘッダを持っていません)。 –

答えて

0

2015 VSでデフォルトの防止でUN-initialiazed変数を使用して、いくつかのコンパイラの警告の設定は、それを使用する前に、intInputを初期化する場合、私は推測すると問題:

int intInput = 0; 
関連する問題