[英]Cin.getline Won't Print Right nor Work Properly
In my following code I am trying to add a line of information into my text file. 在下面的代码中,我试图在文本文件中添加一行信息。 Currently I have tried 3 different options yet none have completely worked.
目前,我尝试了3种不同的选择,但没有一个完全起作用。 (cin.get, cin.getline and cin >>)
(cin.get,cin.getline和cin >>)
When I tried to use the cin.getline(name,60) for instance, It will accept the information but I get a few print out errors (not compiling errors but they display wrong). 例如,当我尝试使用cin.getline(name,60)时,它将接受该信息,但会出现一些打印输出错误(不是编译错误,但它们显示错误)。
1) If I type the assignment as 1 - Web it accepts it in the cin.getline but if I type 1 - Web Design Part 1 Application and I hit enter my program starts looping endlessly of the menu options and doesn't take the information. 1)如果我将分配键入为1-Web,则它在cin.getline中接受它;但是如果我输入1-Web设计第1部分应用程序,然后按Enter键,我的程序将无休止地循环菜单选项,并且不接收信息。
2) When I print my file it prints out weird 2)当我打印文件时,它打印出来很奇怪
ex: 例如:
Accept from the following options to proceed.
1) Add Assignment
2) Remove Assignment
3) Print Listing
4) Quit
1
In our system we show Class information.
Enter class name with class number like so: (MCS 220)
MCS 299
Enter the Project Number < - > then Project title like so: (1 - Web Design Part Application)
1 - Web
Enter the due date for assignment like so: (05-12-2015)
05-12-2015
PRINTS: 打印:
COMPSCI 181,Project 5 - Web page development,04-14-2015
MCS 220,Project 3 - Java fundamentals,04-15-2015
MCS 220,Project 4 - Array, 04-15-2015
, Project CS 299, - We-05-1 // Prints this line
Any help is appreciated! 任何帮助表示赞赏! I can honestly say I am rusty when it comes to reading files.
老实说,当我阅读文件时,我很生锈。 This is the first time I have added to a file so I hope you can explain why I am having this issue and maybe help me fix it.
这是我第一次添加文件,因此希望您能解释为什么我遇到此问题,并可能帮助我解决它。
Below is my current function that adds a line and my main file code. 下面是我当前的函数,它添加了一行和我的主文件代码。
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <cstdlib>
using namespace std;
int options = 0;
char name[60];
//string name; //this was when I attempted cin >>
//string assign; // this was when I attempted cin >>
char assign[60];
string date;
string lines;
void addProject () {
cout <<"Enter class name with class number like so: (MCS 220)"<<endl;
cin.getline(name, 60);
cin.ignore();
//cin >> name;
cout <<endl;
cout <<"Enter the Project Number < - > then Project title like so:" <<
" (1 - Web Design Part Application)"<<endl;
cin.getline(assign, 60);
cin.ignore();
//cin >> assign;
cout <<endl;
cout << "Enter the due date for assignment like so: (05-12-2015)"<<endl;
char cMonth[3];
char cDay [3];
char cYear[5];
cin.get(cMonth,3, '-');
cin.ignore(2, '-');
cin.get(cDay, 4, '-');
cin.ignore(2,'/');
cin.get(cYear, 5);
//input into file
ofstream readFile;
readFile.open("list.txt", std::ios::app);
readFile << name << ", " << "Project " << assign << ", "
<< cMonth <<"-"<< cDay <<"-"<< cYear<< endl;
}
//BEGIN MAIN CODE
int main() {
ifstream readFile("list.txt");
if (!readFile){
cout <<"I am sorry but we could not process "<<
"the file information."<<endl;
}
menu();
cin >>options;
cout <<endl;
cout <<"In our system we show Class information."<<endl;
//read and output the file
while (options != 4)
{
//print the listing
if (options == 3){
while (getline(readFile, lines)){
cout << lines<<endl;
}
}
// Add Assignments
if (options == 1){
addProject();
}
menu();
cin >>options;
cout <<endl;
}//end while
readFile.close();
}
I updated my code. 我更新了我的代码。 The issue was having the cin.ignore placed right by next cin variable.
问题在于将cin.ignore放置在下一个cin变量的右边。
char name[256];
char pnum[20];
char title[256];
string date;
string lines;
void addProject () {
cout <<"Enter class name with class number like so: (MCS 220)"<<endl;
cin.ignore(numeric_limits<std::streamsize>::max(), '\n');
cin.getline(name, 256, '\n');
//char upperName = toupper(name); /*
for (char *caps = name; *caps != '\0'; ++caps)
{
*caps = std::tolower(*caps);
++caps;
}
cout <<endl;
cout <<"Enter the Project Number: "<<endl;
cin.getline(pnum, 20, '\n');
cin.ignore(numeric_limits<std::streamsize>::max(), '\n');
cout<<"Enter the Project's title next: "<<endl;
cin.getline(title, 256, '\n');
cin.ignore(numeric_limits<std::streamsize>::max(), '\n');
cout <<endl;
cout << "Enter the due date for assignment like so: (05-12-2015)"<<endl;
char cMonth[3];
char cDay [3];
char cYear[5];
cin.get(cMonth,3, '-');
cin.ignore(2, '-');
cin.get(cDay, 4, '-');
cin.ignore(2, '-');
cin.get(cYear,5);
//input into file
ofstream readFile;
readFile.open("list.txt", std::ios::app);
readFile << name <<",Project "<< pnum << " - " << title <<
", "<< cMonth <<"-" <<cDay <<"-"<< cYear<< endl;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.