关于c++中的输出有关问题,求解
关于c++中的输出问题,求解啊
这是直接输入回车的结果,没有问题的。
这是自己输入的结果,就是为什么要输入2次6才能出现式子了,怎么解决了。。
------解决方案--------------------
cout<<"请输入要做的题数(有2种选择,第一种是默认题数100道,另外的是自己输入的):";
if(getchar()!='\n')
{
cin>>total;
}.......
getchar()一次输入也就是第一次6(其实这一次输入什么都行)
cin>>total;试一次输入,这次要输入6
我没调试,看代码应该吧if(getchar()!='\n')这个判断去掉
------解决方案--------------------
代码贴全了,光看这个看不出问题在哪里,可能是getchar()太多了也不一定。
------解决方案--------------------
你应该单步调一下
------解决方案--------------------
第一个6给getchar()了,当然还要再输一次了
改为
------解决方案--------------------
//cin.get()用来干嘛?
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
#include <time.h>
using namespace std;
int menu()
{
int choice;
cout<<"************"<<endl;
cout<<"*1 加法计算*"<<endl;
cout<<"*2 减法计算*"<<endl;
cout<<"*3 乘法计算*"<<endl;
cout<<"*4 除法计算*"<<endl;
cout<<"*5 退 出*"<<endl;
cout<<"************"<<endl;
cout<<"Enter a number between 1 and 5:";
cin>>choice;
return choice;
}
void main()
{
ofstream ocout("1.txt");
if(!ocout)//判断打开文件是否成功
{
cout<<"不能打开文件:"<<"1.txt"<<'\n';
return;
}
srand((unsigned)time(NULL));
int count=0,okanswer=0;//分别记录每行的等式及正确的题数
int flag,index,choice,num1,num2,total=100;
int *result=new int[total];
int *ianswer=new int[total];
*result=0;*ianswer=0;//分别初始化2个指针
choice=menu();
if (choice==5)
{
cout<<"拜拜啦!"<<endl;
exit(1);
}
cin.get();
cout<<"请输入要做的题数(有2种选择,第一种是默认题数100道,另外的是自己输入的):";
if(getchar()!='\n')
{
cin>>total;
}.......
c++
------解决方案--------------------
cout<<"请输入要做的题数(有2种选择,第一种是默认题数100道,另外的是自己输入的):";
if(getchar()!='\n')
{
cin>>total;
}.......
getchar()一次输入也就是第一次6(其实这一次输入什么都行)
cin>>total;试一次输入,这次要输入6
我没调试,看代码应该吧if(getchar()!='\n')这个判断去掉
------解决方案--------------------
代码贴全了,光看这个看不出问题在哪里,可能是getchar()太多了也不一定。
------解决方案--------------------
你应该单步调一下
------解决方案--------------------
if(getchar()!='\n')
{
cin>>total;
}
第一个6给getchar()了,当然还要再输一次了
改为
char c;
if((c=getchar())!='\n')
{
total=c;
}
------解决方案--------------------
choice=menu();
if (choice==5)
{
cout<<"拜拜啦!"<<endl;
exit(1);
}
cin.get();
cout<<"请输入要做的题数(有2种选择,第一种是默认题数100道,另外的是自己输入的):";
//cin.get()用来干嘛?