我想检查用户输入的值是字符串还是整数。输入在代码中被视为 cin>> obj [i] .int_code; 。
问题在于,如果用户输入任何字符串而不是整数,程序将进入无限循环。
#include<iostream>
using namespace std;
class item{
public:
int int_code;
string str_name;
};
item obj[100];
main(){
int c,i=0,int_cd,n=0,pos=0;
do{
cout<<"\n_______YOUR OPTIONS________\n";
cout<<"\t1.Add Item\n\t2.View Item\n\t3.Delete Item\n\t4.List Item\n\t5.Exit\n";
cout<<"Enter your choice: ";
cin>>c;
switch(c){
case 1://Add Item
cout<<"Enter the item_code: ";
cin>>obj[i].int_code;
cout<<"Enter the item_name: ";
cin>>obj[i].str_name;
i++;
n++;
break;
case 2://View item
cout<<"Enter the item_code: ";
cin>>int_cd;
for(i = 0;i < n;i++)
if(int_cd == obj[i].int_code)
cout<<obj[i].int_code<<"\t"<<obj[i].str_name<<"\n";
break;
case 3://Delete Item
cout<<"Enter the item_name: ";
cin>>int_cd;
for(i = 0;i < n; i++)
if(int_cd==obj[i].int_code)
pos=i;
for(i=pos;i<n;i++){
if(obj[i].int_code != NULL)
obj[i]=obj[i+1];
else
obj[i].int_code=NULL;
//obj[i].str_name=;
}
n--;
break;
case 4://list Item
for(i=0;i<n;i++)
cout<<obj[i].int_code<<"\t"<<obj[i].str_name<<"\n";
break;
default:
cout<<"Enter any number between 1 to 5";
break;
}
}while( c != 5 );
}
enter code here
答案 0 :(得分:2)
string str;
cin >> str;
int val;
try
{
val = lexical_cast<int>(str));
}
catch(const bad_lexical_cast &)
{
// not an int
}
答案 1 :(得分:1)
更改行
来自
cin>>c;
到
c = -1;
cin>>c;
if (c < 1 || c > 5)
{
cout<<"Enter any number between 1 to 5";
continue;
}
答案 2 :(得分:1)
作为lexical_cast
的替代方案,您可以执行以下操作:
#include <iostream>
static const int ZERO_ASCII = 48;
static const int MAX_OPTION = 5;
static const int MIN_OPTION = 1;
int main() {
std::string input;
std::cin >> input;
if(input.length() > 1 ){
std::cout << "Not correct length" << input << std::endl;
return -1; // in your case use "continue"
}
char val = *(input.c_str());
int intVal = static_cast<int>(val);
int option = intVal - ZERO_ASCII; // see your printed out ASCII chart :)
if (MIN_OPTION > option || option > MAX_OPTION) {
std::cout << "Not valid value " << option << std::endl;
return -1; // in your case use "continue"
}
std::cout << "selected option " << option << std::endl;
}
这是一个有效的选择,因为有几点:
答案 3 :(得分:0)
非数字时退出:
#include<iostream>
using namespace std;
class item{
public:
int int_code;
string str_name;
};
item obj[100];
main(){
int c,i=0,int_cd,n=0,pos=0;
do{
cout<<"\n_______YOUR OPTIONS________\n";
cout<<"\t1.Add Item\n\t2.View Item\n\t3.Delete Item\n\t4.List Item\n\t5.Exit\n";
cout<<"Enter your choice: ";
cin >> c;
if (!cin) break;
switch(c){
case 1://Add Item
cout<<"Enter the item_code: ";
cin>>obj[i].int_code;
cout<<"Enter the item_name: ";
cin>>obj[i].str_name;
i++;
n++;
break;
case 2://View item
cout<<"Enter the item_code: ";
cin>>int_cd;
for(i = 0;i < n;i++)
if(int_cd == obj[i].int_code)
cout<<obj[i].int_code<<"\t"<<obj[i].str_name<<"\n";
break;
case 3://Delete Item
cout<<"Enter the item_name: ";
cin>>int_cd;
for(i = 0;i < n; i++)
if(int_cd==obj[i].int_code)
pos=i;
for(i=pos;i<n;i++){
if(obj[i].int_code != 0)
obj[i]=obj[i+1];
else
obj[i].int_code= 0;
//obj[i].str_name=;
}
n--;
break;
case 4://list Item
for(i=0;i<n;i++)
cout<<obj[i].int_code<<"\t"<<obj[i].str_name<<"\n";
break;
default:
cout<<"Enter any number between 1 to 5";
break;
}
}while( c != 5 );
}
答案 4 :(得分:0)
请注意&#34;检查输入的值是字符串还是整数&#34;部分是应用程序的多个部分中的问题。即,情况1,2和3都期望整数输入(情况3在下面进一步讨论)。 get_int和get_string方法改编自How to make cin take only numbers。最初我只创建了一个get_int方法;但是,我发现还需要一个get_string,否则&#34;输入你的选择&#34;在&#34; Add Item&#34;,案例1完成后,将会发生两次,而不是一次。
此外,&#34;删除项目&#34;,案例3,在措辞中有错误,因为请求是针对&#34; item_name&#34 ;;但是,cin期望一个整数,而不是一个字符串,所以我修改了那里的措辞以及输入法。我在默认情况下注释掉了cout语句,因为如果先前的情况不适用于输入的值,它现在是多余的。最后,&#34;删除项目&#34;代码仍然有一个错误(在下面修复),因为删除item_code 2的请求会删除item_code 1,如果由于pos没有被重置而只包含item。
#include <iostream>
#include <limits>
#include <sstream>
#include <string>
using namespace std;
class item
{
public:
int int_code;
string str_name;
};
item obj[100];
string get_string(string message)
{
string line = "";
string rs = "";
cout << message;
getline(cin, line);
stringstream ss(line);
ss >> rs;
return rs;
}
int get_int(string message)
{
int ri = -1;
string line = "";
bool isint = false;
while (!isint)
{
cout << message;
getline(cin, line);
stringstream ss(line);
if (ss >> ri)
{
if (ss.eof())
{ // Success
isint = true;
}
}
}
return ri;
}
int main (int argc, char ** argv)
{
int c,i=0,int_cd,n=0,pos=0;
do
{
cout<<"\n_______YOUR OPTIONS________\n";
cout<<"\t1.Add Item\n\t2.View Item\n\t3.Delete Item\n\t4.List Item\n\t5.Exit\n";
c = get_int("Enter your choice: ");
switch(c){
case 1://Add Item
obj[i].int_code = get_int("Enter the item_code: ");
obj[i].str_name = get_string("Enter the item_name: ");
i++;
n++;
break;
case 2://View item
int_cd = get_int("Enter the item_code: ");
for(i = 0;i < n;i++)
if(int_cd == obj[i].int_code)
cout<<obj[i].int_code<<"\t"<<obj[i].str_name.c_str()<<"\n";
break;
case 3://Delete Item
int_cd = get_int("Enter the item_code: ");
pos = -1;
for(i = 0;i < n; i++)
if(int_cd==obj[i].int_code)
pos=i;
// ensure specified item_code is found before deleting an item
if(pos != -1)
{
for(i=pos;i<n;i++)
{
if(obj[i].int_code != NULL)
obj[i]=obj[i+1];
else
obj[i].int_code=NULL;
//obj[i].str_name=;
}
n--;
}
break;
case 4://list Item
for(i=0;i<n;i++)
cout<<obj[i].int_code<<"\t"<< obj[i].str_name.c_str() <<"\n";
break;
default:
//cout<< "Enter any number between 1 to 5" << endl;
break;
}
} while( c != 5 );
return 0;
}