不要将字符串句子c ++作为输入 [英] Doesnot take string sentence c++ as input

查看:121
本文介绍了不要将字符串句子c ++作为输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我从控制台输入一个句子(问题)字符串时,它不会占用整个句子而只需要单词作为(q.ques)





代码



when i input a sentence (question) string from console ,it doesnt take the whole sentence rather takes only word as a ( q.ques )


code

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<string.h>
void main(){
	struct quiz//structure for storing questions
{
	char ques[200];
	char op1[30];
	char op2[30];
	char op3[30];
	char op4[30];
	char z[2];
}q;
ofstream outf("item.csv");
cout<<"enter question :"<<endl;
cin>>q.ques;
outf<<q.ques<<"\n";
cout<<"enter option A :"<<endl;
cin>>q.op1;
outf<<q.op1<<" ";
cout<<"enter option B :"<<endl;
cin>>q.op2;
outf<<q.op2<<" ";
cout<<"enter option C:"<<endl;
cin>>q.op3;
outf<<q.op3<<" ";
cout<<"enter option D :"<<endl;
cin>>q.op4;
outf<<q.op4<<" ";
cout<<"enter correct option :"<<endl;
cin>>q.z;
outf<<q.z<<endl;
outf.close();//end of file for storing in file 
ifstream inf("item.csv");//opening file for reading
inf>>q.ques;
inf>>q.op1;
inf>>q.op2;
inf>>q.op3;
inf>>q.z;
cout<<endl;
cout<<"question"<<endl<<q.ques<<endl;
cout<<q.op1<<" "<<q.op2<<" "<<q.op3<<" "<<q.op4<<endl;
cout<<"correct ansrer no"<<"  "<<q.z<<endl;
getch();
inf.close();//closing file




}//end of main function





我尝试了什么:



i已经试过这个程序在turbo c ++中,作为控制台应用程序



What I have tried:

i have runned this program in turbo c++ ,as a console application

推荐答案

你应该学会尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到有一点它会停止你所期望的。

在Visual Studio 2010中掌握调试 - 初学者指南 [ ^ ]

http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html [ ^ ]

https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html [ ^ ]



使用TC ++调试器运行程序。

在逐行调试程序时跟踪变量的变化。你会看到你的程序到底做了什么。
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

Run your program with TC++ debugger.
Track changes in variables as you debug the program line by line. You will see exactly what your program do.


那是因为 cin 只读取下一个标记。如果你想阅读整个句子,那么你需要调用 cin.getline 。请参阅 basic_istream :: getline [ ^ ]了解详情。
That is because cin only reads the next token. If you want to read the entire sentence then you need to call cin.getline. See basic_istream::getline[^] for full details.


这篇关于不要将字符串句子c ++作为输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆