C++ C++;仅限正整数
这是我第一次使用Stackoverflow。 我正在做一个程序来找出一辆车的MPG。我想知道如何使cin语句只接受正整数?而且,如果您输入了无效的输入,您可以重置它吗?我不确定这是否有意义。我上课不必这么做。我只是好奇怎么做。这是代码C++ C++;仅限正整数,c++,C++,这是我第一次使用Stackoverflow。 我正在做一个程序来找出一辆车的MPG。我想知道如何使cin语句只接受正整数?而且,如果您输入了无效的输入,您可以重置它吗?我不确定这是否有意义。我上课不必这么做。我只是好奇怎么做。这是代码 #include <iostream> using namespace std; int main() { double tank, miles, mpg; cout << "Hello. This is a program that
#include <iostream>
using namespace std;
int main()
{
double tank, miles, mpg;
cout << "Hello. This is a program that calculates the MPG ( Miles Per Gallon) for your\n" ;
cout << "vehicle\n" << endl;
cout << "Please enter how many gallons your vehicle can hold\n" << endl;
cin >> tank;
cout << endl;
cout << "Please enter how many miles that have been driven on a full tank\n" <<endl;
cin >> miles;
cout << endl;
mpg = (miles)/(tank);
cout << "Your vehicle recieves " << mpg << " miles per gallon\n" << endl;
system ("pause");
return 0;
}
#包括
使用名称空间std;
int main()
{
双油箱,英里,每加仑;
coutiostreams不是用于构建复杂用户界面的工具包。除非您想编写自己的相当复杂的流来包装通常的流,否则您无法让它(a)只接受正整数或(b)礼貌地与键入其他内容的用户交互
您应该只读取cin中的行,并在查看得到的内容后打印您自己的错误提示等。而不是
cin >> miles;
试一试
while((cin>>英里)<0)
coutcout不过,在命令行环境中有两种标准方法可以实现这一点
您可以将cin语句困在一个循环中,直到输入有效的输入后才会释放。这是验证CLI输入的“标准”方法,而不仅仅是有符号的数字
do
{
cout << "\nPlease enter...";
cin >> tank;
}
while (tank < 0)
do
{
水槽;
}
而(油箱<0)
while语句中的条件是验证数据的地方。您还可以使用if语句解释输入无效的原因
另一种方法是简单地强制值为正,只需执行tank=fabs(tank);
,它取tank
变量的绝对值(即正)。这是我的无限循环代码
So this is my code for an infinite loop
1: So main will call the "Get_number()" function
2: Get number will accept an int from the user
3(A): If int is greater than 0, go into loop
3(B): Else, display to user "Invalid Input" and then call the function
"Get_number()" again creating an infinite loop until the user
enters a value greater than 0
#include <iostream> // Access the input output stream library
#include <fstream> // Access to the fstream library (used to read and write to files)
#include <chrono> // Needed to access "std::chrono_literals"
#include <thread> // Needed to access "namespace std::this_thread"
using std::fstream; // this will allow us to use the fstream (we'll be able to read and write to files)
using std::ios; // needed for iostream (used to be able to tell fstream to read and/or write to a file and that it's reading/writing a binary file)
using std::cout; // need this statment to access cout (to display info to user)
using std::cin; // need this statment to access cin (to gather info from user)
using std::endl; // need this statment to access endl (will end the line)
using namespace std::this_thread; // This will allow me to use "Sleep_For" or "Sleep_Until"
using namespace std::chrono_literals; // This will allow the use of measurements of time such as ns, us, s, h, etc.
//Prototypes***************************************************************************************************
void shellSort(int read[], int readLength); //Making Prototype (Declaring our function) so that compiler knows not to worry about it
void Get_number();
void Write_to_file(int user_input_of_how_many_random_numbers_to_generate); //Making Prototype (Declaring our function) so that compiler knows not to worry about it
void Read_from_file(int user_input_of_how_many_random_numbers_to_generate);//Making Prototype (Declaring our function) so that compiler knows not to worry about it
//*************************************************************************************************************
void main()
{
Get_number();
system("pause>>void"); // will let the console pause untill user presses any button to continue
}
/**************************************************************************************************************
* Purpose: This function will gather a positive integer from the user and use it to generate that many
* random numbers!
*
* Precondition: None
*
*
* Postcondition:
* Would've gathered the number of random numbers the user wanted to generate and then gone into the
* Write_to_file and Read_from_file function
*
**************************************************************************************************************/
void Get_number()
{
int user_input_of_how_many_random_numbers_to_generate = 0; //make variable that will accept the int value the user wants to generate random numbers
cout << "Please Enter A Number Greater Than Zero:" << endl; // displays to user to enter a number greater than zero
cin >> user_input_of_how_many_random_numbers_to_generate; // will accept the value the user inputted and place it in the "user_input_of_how_many_random_numbers_to_generate" variable
system("cls"); // Will clear the screen
if (user_input_of_how_many_random_numbers_to_generate > 0) // if user input is greater than zero, enter this
{
Write_to_file(user_input_of_how_many_random_numbers_to_generate); // will bring up the "Write_to_file" function
Read_from_file(user_input_of_how_many_random_numbers_to_generate); // will bring up the "Read_from_file" function
}
else // else enter this
{
cout << "invalid input!" << endl; // display to user "invalid input"
sleep_for(2s); // system will pause for 2 seconds allowing the user to read the message of "invalid input"
system("cls"); // console will be cleared
Get_number(); // Get_number function will be entered creating an infinate loop untill the user's input is valid!
}
}
1:So main将调用“Get_number()”函数
2:Get number将接受来自用户的int
3(A):如果int大于0,则进入循环
3(B):否则,向用户显示“无效输入”,然后调用该函数
“Get_number()”再次创建无限循环,直到用户
输入一个大于0的值
#包括//访问输入输出流库
#包括//访问fstream库(用于读取和写入文件)
#包括//访问“std::chrono_文字”所需的
#包括//访问“命名空间std::this_线程”所需的
使用std::fstream;//这将允许我们使用fstream(我们将能够读取和写入文件)
使用std::ios;//iostream需要(用于告诉fstream读取和/或写入文件,并且它正在读取/写入二进制文件)
使用std::cout;//需要此语句访问cout(向用户显示信息)
使用std::cin;//需要此语句访问cin(从用户收集信息)
使用std::endl;//需要此语句访问endl(将结束该行)
使用命名空间std::this_thread;//这将允许我使用“Sleep_For”或“Sleep_Until”
使用名称空间std::chrono_literals;//这将允许使用时间度量,如ns、us、s、h等。
//原型***************************************************************************************************
void shellSort(int read[],int readLength);//制作原型(声明我们的函数),这样编译器就不用担心了
void Get_number();
void Write_to_file(int user_input_of_how_numbers_random_numbers_to_generate);//制作原型(声明我们的函数),这样编译器就不必担心了
void Read_from_file(int user_input_of_how_numbers_random_numbers_to_generate);//制作原型(声明我们的函数),这样编译器就不用担心了
//*************************************************************************************************************
void main()
{
获取_编号();
系统(“暂停>>无效”);//将让控制台暂停,直到用户按下任何按钮继续
}
/**************************************************************************************************************
*用途:此函数将从用户处收集一个正整数,并使用它生成多个正整数
*随机数!
*
*前提条件:无
*
*
*后置条件:
*会收集用户想要生成的随机数,然后进入
*将\u写入\u文件并从\u文件函数中读取\u
*
**************************************************************************************************************/
void Get_number()
{
int user_input of_how_mandom_numbers_to_generate=0;//生成一个变量,该变量将接受用户想要生成随机数的int值
cout user\u input\u of\u how\u numbers\u random\u \u to \u generate;//将接受用户输入的值,并将其放入“user\u input\u of \u how\u numbers\u random\u to \u generate”变量中
系统(“cls”);//将清除屏幕
if(user\u input\u of\u how\u numbers\u random\u numbers\u to\u generate>0)//如果用户输入大于零,请输入
{
写入文件(用户输入要生成的随机数);//将显示“写入文件”功能
从文件读取(用户输入要生成的随机数);//将显示“从文件读取”功能
}
else//else输入这个
{
不能回答你的问题,但如果你想要整数,你不应该要求双倍。先要一个整数,然后处理它不是正的事实。更好的方法是读取字符串,然后解析它们,以确保这是必需的/如果有人输入了不需要的内容,则标记错误,并给他们另一个opp机会。是的,你不能从键盘上实际移除键。人们会犯输入错误。表达式cin>>miles
返回cin
,因此我不确定这应该如何工作。这也不会对用户进行任何错误检查
do
{
cout << "\nPlease enter...";
cin >> tank;
}
while (tank < 0)
So this is my code for an infinite loop
1: So main will call the "Get_number()" function
2: Get number will accept an int from the user
3(A): If int is greater than 0, go into loop
3(B): Else, display to user "Invalid Input" and then call the function
"Get_number()" again creating an infinite loop until the user
enters a value greater than 0
#include <iostream> // Access the input output stream library
#include <fstream> // Access to the fstream library (used to read and write to files)
#include <chrono> // Needed to access "std::chrono_literals"
#include <thread> // Needed to access "namespace std::this_thread"
using std::fstream; // this will allow us to use the fstream (we'll be able to read and write to files)
using std::ios; // needed for iostream (used to be able to tell fstream to read and/or write to a file and that it's reading/writing a binary file)
using std::cout; // need this statment to access cout (to display info to user)
using std::cin; // need this statment to access cin (to gather info from user)
using std::endl; // need this statment to access endl (will end the line)
using namespace std::this_thread; // This will allow me to use "Sleep_For" or "Sleep_Until"
using namespace std::chrono_literals; // This will allow the use of measurements of time such as ns, us, s, h, etc.
//Prototypes***************************************************************************************************
void shellSort(int read[], int readLength); //Making Prototype (Declaring our function) so that compiler knows not to worry about it
void Get_number();
void Write_to_file(int user_input_of_how_many_random_numbers_to_generate); //Making Prototype (Declaring our function) so that compiler knows not to worry about it
void Read_from_file(int user_input_of_how_many_random_numbers_to_generate);//Making Prototype (Declaring our function) so that compiler knows not to worry about it
//*************************************************************************************************************
void main()
{
Get_number();
system("pause>>void"); // will let the console pause untill user presses any button to continue
}
/**************************************************************************************************************
* Purpose: This function will gather a positive integer from the user and use it to generate that many
* random numbers!
*
* Precondition: None
*
*
* Postcondition:
* Would've gathered the number of random numbers the user wanted to generate and then gone into the
* Write_to_file and Read_from_file function
*
**************************************************************************************************************/
void Get_number()
{
int user_input_of_how_many_random_numbers_to_generate = 0; //make variable that will accept the int value the user wants to generate random numbers
cout << "Please Enter A Number Greater Than Zero:" << endl; // displays to user to enter a number greater than zero
cin >> user_input_of_how_many_random_numbers_to_generate; // will accept the value the user inputted and place it in the "user_input_of_how_many_random_numbers_to_generate" variable
system("cls"); // Will clear the screen
if (user_input_of_how_many_random_numbers_to_generate > 0) // if user input is greater than zero, enter this
{
Write_to_file(user_input_of_how_many_random_numbers_to_generate); // will bring up the "Write_to_file" function
Read_from_file(user_input_of_how_many_random_numbers_to_generate); // will bring up the "Read_from_file" function
}
else // else enter this
{
cout << "invalid input!" << endl; // display to user "invalid input"
sleep_for(2s); // system will pause for 2 seconds allowing the user to read the message of "invalid input"
system("cls"); // console will be cleared
Get_number(); // Get_number function will be entered creating an infinate loop untill the user's input is valid!
}
}