C++ 它不会打印根据用户输入c++;
我现在的问题是,它没有根据用户输入打印出输入。例如: 在我插入了我想在日历中添加日程安排的日期之后,我输入的任务是“C++”,但程序没有任何结果。我希望它能打印“C++”,我认为问题应该在这一部分:C++ 它不会打印根据用户输入c++;,c++,C++,我现在的问题是,它没有根据用户输入打印出输入。例如: 在我插入了我想在日历中添加日程安排的日期之后,我输入的任务是“C++”,但程序没有任何结果。我希望它能打印“C++”,我认为问题应该在这一部分: for (;;) { cout << "You task: " << endl; string sunday; if (!getline(cin,sunday) || sunday.empty())break; sunday_task.push_back(su
for (;;) {
cout << "You task: " << endl;
string sunday;
if (!getline(cin,sunday) || sunday.empty())break;
sunday_task.push_back(sunday);
for (int i = 0; i<sunday_task.size(); i++){ // This part does not work
cout << sunday_task[i] << endl; // This part does not work
}
^^在这一部分之后,我希望它打印出“C++”
很抱歉,因为我是StackOverflow新手,我希望我已经解释清楚了,并提供了足够的信息。不相关的问题:
string login()
正如我在相邻的评论中提到的,如果你承诺使用字符串,你必须返回一个,否则会发生不好的事情。或者将函数更改为void。始终在启用警告的情况下编译,尝试修复它们
你的问题是
cin>>string\u variable
和cin>>char\u variable
在cin
中留下一个未处理的换行符,因此下次执行getline时,会得到一个空字符串。
放入类似
字符串温度的内容;getline(cin,temp)代码>后cin>>选择代码>和cin>>天后
来吃那条额外的新线。请阅读:@DSC您可以使用[mcve]
生成一个更好的链接,它变成:@melpomene谢谢。我不知道。我将页面另存为书签。@这里有一大堆神奇的链接,请尝试[edit]
或[ask]
并尝试对您的问题做出更好的描述,而不是“它没有达到我预期的效果”。我们不知道它做了什么(告诉我们)和你期望什么(也告诉我们)。感谢与此无关的问题部分,它解决了这个错误。在我尝试放置它之后,它向我显示了这个错误:调用“getline(std::istream&,char&)”getline(cin,choice)没有匹配的函数@DuxtonLim尝试istream::ignore
跳过额外的换行符。
#include <iostream>
#include <windows.h>
#include <vector>
#include <time.h>
#include <string>
#include <algorithm>
#include <stack>
using namespace std;
string login() {
start:
cout << "Please login to access your account" << endl;
cout << "Please enter your username or email: " ;
string username;
getline (cin,username);
if (username == "duxton"){
cout << "Please enter your password:";
string password;
getline (cin, password);
if (password == "password") {
cout << "You have successfully login" << endl;
char choice;
cout << "\n\nChoose one of the choice below:\n"
<< "a. Add a schedule to your calender\n"
<< "b. View your schedule for the month\n"
<< "c. Exit\n\n";
//Get the choice
cout << "Insert Option:";
cin >> choice;
// potential to be use in struct
vector <string> sunday_task ;
string days;
switch (choice)
{
case 'a': cout <<"Sunday Monday Tuesday Wednesday Thursday Friday Saturday" << endl;
cout << "Which day would you like to insert task to? " << endl;
cout << "Insert day : " ;
cin >> days;
if (days == "Sunday" or "sunday")
{
for (;;) {
cout << "You task: " << endl;
string sunday;
if (!getline(cin,sunday) || sunday.empty())break;
sunday_task.push_back(sunday);
for (int i = 0; i<sunday_task.size(); i++){
cout << sunday_task[i] << endl;
}
}
}
break;
case 'b': cout << "View calender" << endl;
break;
case 'c': break;
default:
cout << "Input Error!";
}
}
else {
cout << "Login failed" << endl; goto start;
}
}
else { cout << "Login failed" << endl; goto start;}
cin.get();
}
int main(){
login();
return 0;
}
Please login to access your account
Please enter your username or email: duxton
Please enter your password: password
You have successfully login
Choose one of the choice below:
a. Add a schedule to your calender
b. View your schedule for the month
c. Exit
Insert Option:a
Sunday Monday Tuesday Wednesday Thursday Friday Saturday
Which day would you like to insert task to?
Insert day : sunday
You task: c++