标识符" friends_name"未定义&标识符"输入"未定义? C ++

时间:2018-04-02 21:17:36

标签: c++ visual-studio input undefined

我试图提示用户输入他或她的名字。 我写了#34; hello,first_name",其中first_name是用户输入的名称。 然后我按如下方式修改了代码:将提示更改为"输入要写入的人的姓名"并将输出更改为"你好,first_name,"你喜欢你现在生活中的哪个地方(y / n)?&#34 ;; 这是在我写一个if语句之前,让用户决定要执行什么代码,但是在给定的条件下输入"条件"我没有工作,因为它没有定义,尽管我把它们分配给了' y'并且' n'。 简而言之,我完全不了解如何定义first_name和输入,我试图让用户输入要分配给这些变量的值。 The Error list

 #include "stdafx.h"
 #include "iostream"
 #include"string"
 using namespace std;

 int main()
     {
string first_name;
cout << " Please enter your first name (followed by 'enter'):\n";
cin >> first_name;
    cout << "Hello," << first_name << " do you like where you are right now in life (y/n)?:\n";
    cin >> first_name;
    if (input =='y') {
        cout << "what do you like about it over,the people?(y/n)";//?:\n"

    }
    else if (input == 'n') {
        cout << " what do you hate about it,the expenses?(y/n)"//?:\n"
    }
    else cout << "invalid choice";
    cout << " what was your friends name again ?" << endl;
    string = friends_name;
    cin >> friends_name;
}
return 0;
     }
   }

1 个答案:

答案 0 :(得分:0)

在你的程序的第12行,看起来你已经使用变量first_name而不是输入。将“first_name”替换为“input”。 例如:

cin>>input

同样如评论中所述,请确保声明您使用的所有变量

例如:

string first_name;
string input;
string friends_name;

编辑的工作代码如下:

 #include "stdafx.h"
 #include "iostream"
 #include"string"
 using namespace std;

int main(){
 string first_name;
 string friends_name;
 string input;
 cout << " Please enter your first name (followed by 'enter'):\n";
 cin >> first_name;
 cout << "Hello," << first_name << " do you like where you are right now in life (y/n)?:\n";
 cin >> input;
 if (input =="y")
     cout << "what do you like about it over,the people?(y/n)";//?:\n"
 else if (input == "n")
    cout << " what do you hate about it,the expenses?(y/n)";//?:\n"
 else cout << "invalid choice";
 cout << " what was your friends name again ?" << endl;
 cin >> friends_name;
 return 0;
 }