我无法在 C++ 中用 'cout' 打印出句子
I can't print out sentence with 'cout' in c++
首先我的母语不是英语,所以如果您发现我的英语有错误请忽略它们,
在我的代码下方
#include <iostream>
#include <string>
#include <vector>
using namespace std;
//Globle variables
int Counter = 0 ;
vector<string> tasksList;
string task;
//functions
int AddingTasks(){
cout<<"type your task here: ";
cin>>task;
tasksList[Counter-1].assign(task); // the "Counter-1" to set the index to zero because 'Counter' variable = 1
cout<<tasksList[Counter-1]<<"added "<<endl; // the problem is here it crash here it won't print out this Sentence
return 0;
}
int main(int argc, char const *argv[])
{
tasksList.resize(Counter);
int ChooseNum;
cout<<"Mustafa ToDoList the best to do list ever! "<<endl;
cout<<"Choose: [1] add task [2] view tasks [3] edit task [4] delete task : ";
cin>>ChooseNum;
if (ChooseNum == 1)
{
Counter++;
AddingTasks();
}
// [2][3][4] are unnecessary now
return 0;
}
我认为我的代码没有错误,但是 'AddingTasks()' 函数有问题程序在尝试打印已添加的任务时崩溃,我不知道为什么?
我是 C++ 初学者。
更改添加任务:
int AddingTasks()
{
cout << "type your task here: ";
cin >> task;
tasksList.push_back(task);
cout << tasksList[tasksList.size() - 1] << " added " << endl;
return 0;
}
如果您尝试访问矢量中不存在的对象,会发生什么情况。
首先我的母语不是英语,所以如果您发现我的英语有错误请忽略它们,
在我的代码下方
#include <iostream>
#include <string>
#include <vector>
using namespace std;
//Globle variables
int Counter = 0 ;
vector<string> tasksList;
string task;
//functions
int AddingTasks(){
cout<<"type your task here: ";
cin>>task;
tasksList[Counter-1].assign(task); // the "Counter-1" to set the index to zero because 'Counter' variable = 1
cout<<tasksList[Counter-1]<<"added "<<endl; // the problem is here it crash here it won't print out this Sentence
return 0;
}
int main(int argc, char const *argv[])
{
tasksList.resize(Counter);
int ChooseNum;
cout<<"Mustafa ToDoList the best to do list ever! "<<endl;
cout<<"Choose: [1] add task [2] view tasks [3] edit task [4] delete task : ";
cin>>ChooseNum;
if (ChooseNum == 1)
{
Counter++;
AddingTasks();
}
// [2][3][4] are unnecessary now
return 0;
}
我认为我的代码没有错误,但是 'AddingTasks()' 函数有问题程序在尝试打印已添加的任务时崩溃,我不知道为什么?
我是 C++ 初学者。
更改添加任务:
int AddingTasks()
{
cout << "type your task here: ";
cin >> task;
tasksList.push_back(task);
cout << tasksList[tasksList.size() - 1] << " added " << endl;
return 0;
}
如果您尝试访问矢量中不存在的对象,会发生什么情况。