C++ 如何在C++;?
好的,我已经找了好几天了,但是我找不到任何有用的 我有一个程序,我想确保用户输入的是整数而不是双精度 这个程序运行得很好,但我需要验证numOne和numTwo,以确保它们是整数而不是双精度的(5.5)C++ 如何在C++;?,c++,C++,好的,我已经找了好几天了,但是我找不到任何有用的 我有一个程序,我想确保用户输入的是整数而不是双精度 这个程序运行得很好,但我需要验证numOne和numTwo,以确保它们是整数而不是双精度的(5.5) intmain() {//这是存储变量的地方 int numOne,numTwo,answer,right answer,ranNumOne,ranNumTwo; //这将向用户显示以输入要使用的数字范围 库特·努蒙) { cout numTwo) { 不能使用std:n:ci.fail()查看
intmain()
{//这是存储变量的地方
int numOne,numTwo,answer,right answer,ranNumOne,ranNumTwo;
//这将向用户显示以输入要使用的数字范围
库特·努蒙)
{
cout numTwo)
{
不能使用std:n:ci.fail()
查看它是否失败
int numOne;
cin >> numOne;
if(cin.fail())
cout << "Not a number...")
intnumone;
金银花;
if(cin.fail())
cout>var;
while(std::cin.fail()){
std::cin.clear();
标准::cin.ignore(256,“\n”);
std::cout>var;
}
收益var;
}
或否:
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <string>
#include <ctime>
#define DIFF(n1, n2) (n1 > n2 ? n1 - n2 : n2 - n1)
using namespace std;
int input(const string &firstmsg = "", const string &errmsg = "") {
int var;
std::cout << firstmsg;
std::cin >> var;
while (cin.fail()) {
cin.clear();
cin.ignore(256, '\n');
cout << errmsg;
cin >> var;
}
return var;
}
int main(){
//This is where my variables are stored
int numOne, numTwo, answer, rightAnswer, ranNumOne, ranNumTwo;
//this will display to the user to enter a range of numbers to be used
cout << "Please enter a set of numbers to be the range for the problems." << endl << endl;
numOne = input("Please enter the beginning number: ", "Invalid. Enter again: ");
//this asks the user for the second number
numTwo = input("Please enter the ending number: ", "Invalid. Enter again: ");
//this is where the first number is generated
srand(time(0));
ranNumOne = rand() % (DIFF(numOne, numTwo)) + 1; // ensures it will always be positive
system("PAUSE");
//this is where the second number is generated
srand(time(0));
ranNumTwo = rand() % (DIFF(numOne, numTwo)) + 1;
//this is where the calculations are done
rightAnswer = ranNumOne + ranNumTwo;
//this displays the problem that was generated
cout << "What is: " << endl;
cout << setw(11) << ranNumOne << endl;
cout << setw(6) << "+" << setw(3) << ranNumTwo << endl;
cout << " -------\n";
cin >> answer;
//this checks to see if the answer is right or not and displays the result
if (answer == rightAnswer){
cout << "Your answer was correct! " << endl;
}
else
cout << "The correct answer is: " << rightAnswer << endl;
return 0;
}
#包括
#包括
#包括
#包括
#包括
#定义差异(n1,n2)(n1>n2?n1-n2:n2-n1)
使用名称空间std;
int输入(常量字符串&firstmsg=“”,常量字符串&errmsg=“”){
int-var;
std::cout>var;
while(cin.fail()){
cin.clear();
cin.ignore(256,“\n”);
cout>var;
}
收益var;
}
int main(){
//这是我的变量存储的地方
int numOne,numTwo,answer,right answer,ranNumOne,ranNumTwo;
//这将向用户显示以输入要使用的数字范围
cout为什么不呢,把数字转换成一个双精度,然后看看这个双精度是不是一个整数
double d;
cin>>d;
if (ceil(d) != d)
cout >> " not an integer";
我尝试了许多不同的方法,但都没有效果。你的程序要求两次输入数字,一次不进行检查,一次进行检查。用户不输入整数或双精度。他们输入数字。考虑到你正在读取的类型,这些数字会被尽可能灵活地解析。我必须将数字输入两次才能运行RKI知道,我知道…它很可爱,不是吗?为什么不写一个好的函数?STD有什么用?:程序需要它还是可以删除?我在C++年1,程序工作到用户输入5.5,这是一个双而不是整数。它已经验证了我需要的数量,以确保它是一个整数。ger@DannyArnold它指定了名称空间。如果您在上面的程序中使用using namespace std;
,您可以删除它。我不知道所有花哨的代码,我只知道基本的…我还知道我在那里的代码可以工作,它只需要微调…我需要知道为什么我必须输入两次数字以及如何修复它…我喜欢它我得到了帮助,但是高级代码现在帮不了我。这很简单:-)这里发布的东西都不管用,出现了错误。我的代码管用,但是需要用户输入两次数字…为什么?
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <string>
#include <ctime>
#define DIFF(n1, n2) (n1 > n2 ? n1 - n2 : n2 - n1)
using namespace std;
int input(const string &firstmsg = "", const string &errmsg = "") {
int var;
std::cout << firstmsg;
std::cin >> var;
while (cin.fail()) {
cin.clear();
cin.ignore(256, '\n');
cout << errmsg;
cin >> var;
}
return var;
}
int main(){
//This is where my variables are stored
int numOne, numTwo, answer, rightAnswer, ranNumOne, ranNumTwo;
//this will display to the user to enter a range of numbers to be used
cout << "Please enter a set of numbers to be the range for the problems." << endl << endl;
numOne = input("Please enter the beginning number: ", "Invalid. Enter again: ");
//this asks the user for the second number
numTwo = input("Please enter the ending number: ", "Invalid. Enter again: ");
//this is where the first number is generated
srand(time(0));
ranNumOne = rand() % (DIFF(numOne, numTwo)) + 1; // ensures it will always be positive
system("PAUSE");
//this is where the second number is generated
srand(time(0));
ranNumTwo = rand() % (DIFF(numOne, numTwo)) + 1;
//this is where the calculations are done
rightAnswer = ranNumOne + ranNumTwo;
//this displays the problem that was generated
cout << "What is: " << endl;
cout << setw(11) << ranNumOne << endl;
cout << setw(6) << "+" << setw(3) << ranNumTwo << endl;
cout << " -------\n";
cin >> answer;
//this checks to see if the answer is right or not and displays the result
if (answer == rightAnswer){
cout << "Your answer was correct! " << endl;
}
else
cout << "The correct answer is: " << rightAnswer << endl;
return 0;
}
double d;
cin>>d;
if (ceil(d) != d)
cout >> " not an integer";