2017-09-05 26 views
-2

我正在创建一个程序,询问用户的回复以及要打印回复并显示它的次数。我在程序中使用了一个while循环和开关盒。 但是,当我在std::cin的帮助下将输入存储在变量a中时,同一输入未被开关盒接收到。任何帮助表示赞赏。用cin输入在开关盒中不工作

#include <iostream> 

using namespace std; 

int a; 
int input; 
int i=1; 
void display() 
{ 
    cout << "Select a choice for reply" << endl; 
    cout << "1.Thank You" << endl; 
    cout << "2.Welcome" << endl; 
    cout << "3.Okay" << endl; 
} 

int main() 
{ 
    display(); 
    cout << "Enter Choice" << endl; 
    cin >> a; 
    input='a'; 
    switch (input) 
    { 
     case '1': { 
      int x; 
      cout << "Enter no. of times you want to print reply line" << endl; 
      cin >> x; 
      while (i <= x) 
      { 
       cout << "Thank you" << endl; 
      } 

      break; 
     } 
     case '2': { 
      int x; 
      cout << "Enter no. of times you want to print line" << endl; 
      cin >> x; 
      while (i <= x) 
      { 
       cout << "Welcome" << endl; 
      } 

      break; 
     } 
     case '3': { 
      int x; 
      cout << "Enter no. of times you want to print line" << endl; 
      cin >> x; 
      while (i <= x) 
      { 
       cout << "okay" << endl; 
      } 

      break; 
     } 
     default: { 
      cout << "wrong choice" << endl; 
     } 

     cout << "Thank you for replying" << endl; 
    } 

    return 0; 
} 
+1

'case'1'' - um。很少有机会为你工作。我不假设你试过'案例1'(即不要使用字符文字)。既然'i'和'x'都不会在你的'while'循环中改变,即使你解决了第一个问题,它也注定要旋转到无穷大。我*强烈*建议[关于C++的好书](https:// stackoverflow。COM /问题/ 388242 /对,最终-C-书指南和列表)。 – WhozCraig

+0

请正确格式化您的代码,就像您C++教科书中的示例一样。 –

+0

您声明'input'为'integer',然后分配'input ='a''。之后你把'switch(input)'。 – kiLLua

回答

3

的核心问题是:

input = 'a' 

和:

case '1': 

你可能想要的是:

input = a 

case 1: 

毕竟编译器应该扔了一个警告。 但为什么要复制这个值呢? 只是做

switch(a) 

其实,有许多问题与您的代码,但我会让其他人阐述这一点。

+0

谢谢我刚刚开始编码,我很感谢你的帮助 –

0

你的问题是input='a';,输入值是97,这是相同的,如果你写input = (int)'a';

你为什么要声明“A”变量?只要使用输入 cin >> input;

0
#include <iostream> 
using namespace std; 
int a; 
int input; 
int i=1; 
void display() 
{ 
    cout<<"Select a choice for reply"<<endl; 
    cout<<"1.Thank You"<<endl; 
    cout<<"2.Welcome"<<endl; 
    cout<<"3.Okay"<<endl; 
} 

int main() 
{ 
    display(); 
    cout<<"Enter Choice"<<endl; 
    cin>>a; 
    input=a; 
    switch(input) 
    { 
     case 1 : 
     { 
      int x; 
      cout<<"Enter no. of times you want to print reply 
      line<<endl; 
      cin>>x; 
      while(i<=x) 
      { 
       cout<<"Thank you"<<endl; 
       x--; 
      } 
      break; 
     } 
     case 2 : 
     { 
      int x; 
      cout<<"Enter no. of times you want to print line" <<endl; 
      cin>>x; 
      while(i<=x) 
      { 
       cout<<"Welcome"<<endl; 
       x--; 
      } 
      break; 
     } 
     case 3 : 
     { 
      int x; 
      cout<<"Enter no. of times you want to print line"<<endl; 
      cin>>x; 
      while(i<=x) 
      { 
       cout<<"okay"<<endl; 
       x--; 
      } 
      break; 
     } 
     default: 
     { 
      cout<<"wrong choice"<<endl; 
     } 
     cout<<"Thank you for replying"<<endl; 
    } 
    return 0; 
} 
+0

你的问题是input ='a';输入值将是97,如果你写input =(int)'a',它是一样的;同样没有必要采取两个不同的变量是一个和输入,一个就足够了。但是既然你声明和使用了这两个变量,我并没有从你的程序中删除这个变量。然后总是记住整数的语法是(情况1:),其中1可以被你提供的任何数字替代,作为字符的选择和语法(case'a':)。在while循环中,你忘记给出终止条件X - ;)。 –

0

你在你的代码的几个问题:
1)你试图给一个整数的字符:input = 'a';
去做正确的,你应该直接使用变量:input = a;(没有' )

2)在你的开关情况下,你与'1','2'(类型字符)进行比较,但input是整数类型。试试这个:
case 1:case 2:(无')

3)你不会在你的while循环中增加/减少i或x,它们永远不会停止。试试类似于:i++x--您的循环中的某处。

一些言论:
1)您可以使用开关的情况下没有大括号({}):

int a = 0; 
cin >> 1; 
switch(a) 
{ 
    case 1: 
     break; 
    case 2: 
     int tmp = 0; 
     cout<<"tmp: "<<tmp<<endl; 
     break; 
    default: 
     break; 
} 

2)尽量避免全局变量,它们在大多数情况下,没有必要的,不好的做法。

3)你不需要你的变量a,你可以直接使用input,如:

cout << "Enter Choice" << endl; 
cin >> input; 
switch (input) 

4)最后但并非最不重要的,我建议你读一些好的C++的书,这是真的值得的。 (Good books