我创建了一个程序,允许用户在一周内输入数据,并允许他们选择他们想要在哪一天插入数据的任务数量。
用户可以在几天内查看他们的输入,或者它将在一周内显示。
我试过这个:
cout << sunday_task << ' ' << sunday_time << ' ' << monday_task << ' ' <<
monday_time << ' ' << tuesday_task << ' ' << tuesday_time << ' ' <<
wednesday_task << ' ' << wednesday_time << ' ' << thursday_task << ' ' <<
thursday_time << ' ' << friday_task << ' ' << friday_time << ' ' <<
saturday_task << ' ' << saturday_time << endl;
这给了我多字符字符常量 [-wmultichar] 的错误
if (calendar_days == "Sunday" || calendar_days == "sunday") {
cout << "Task: Time: " << endl;
work.print_task_sunday();
to_restart();
inside_days();
}
else if (calendar_days == "Monday" || calendar_days == "monday") {
cout << "Task: Time: " << endl;
work.print_task_monday();
to_restart();
inside_days();
}
else if (calendar_days == "Tuesday" || calendar_days =="tuesday"){
cout << "Task: Time: " << endl;
work.print_task_tuesday();
to_restart();
inside_days();
}
else if (calendar_days == "Wednesday" || calendar_days == "wednesday") {
cout << "Task: Time: " << endl;
work.print_task_wednesday();
to_restart();
inside_days();
}
else if (calendar_days == "Thursday" || calendar_days == "thursday") {
cout << "Task: Time: " << endl;
work.print_task_thursday();
to_restart();
inside_days();
}
else if (calendar_days == "Friday" || calendar_days == "friday") {
cout << "Task: Time: " << endl;
work.print_task_friday();
to_restart();
inside_days();
}
else if (calendar_days == "Saturday" || calendar_days == "saturday") {
cout << "Task: Time: " << endl;
work.print_task_saturday();
to_restart();
inside_days();
}
else if (calendar_days == "entire week" || calendar_days == "Entire week" ||
calendar_days == "Entire Week") {
string sunday_task;
int sunday_time;
string monday_task;
int monday_time;
string tuesday_task;
int tuesday_time;
string wednesday_task;
int wednesday_time;
string thursday_task;
int thursday_time;
string friday_task;
int friday_time;
string saturday_task;
int saturday_time;
ifstream file1("sunday_task.txt", ios::in);
ifstream file2("monday_task.txt", ios::in);
ifstream file3("tuesday_task.txt", ios::in);
ifstream file4("wednesday_task.txt", ios::in);
ifstream file5("thursday_task.txt", ios::in);
ifstream file6("friday_task.txt", ios::in);
ifstream file7("saturday_task.txt", ios::in);
ofstream ofile("all_task.txt", ios::out | ios::app);
ofile << file1.rdbuf() << file2.rdbuf() << file3.rdbuf() <<
file4.rdbuf() << file5.rdbuf() << file6.rdbuf() << file7.rdbuf();
cout << sunday_task << ' ' << sunday_time << ' ' << monday_task << ' ' <<
monday_time << ' ' << tuesday_task << ' ' << tuesday_time << ' ' <<
wednesday_task << ' ' << wednesday_time << ' ' << thursday_task << ' ' <<
thursday_time << ' ' << friday_task << ' ' << friday_time << ' ' <<
saturday_task << ' ' << saturday_time << endl;
预期结果是打印出来
sundaytask1 930 mondaytask1 930 tuesdaytask1 930 wednesdaytask1 930 thursdaytask1 930 fridaytask1 930 saturdaytask1 930
但取而代之的是,它给了我随机数。
这是它给我的输出1878014496 10 4239492 1878014016 1877936777 6422120 3313664
功能
void to_restart() {
cout << "Would you like to quit or to view calendar?" << endl;
cout << "Answer quit or calendar? : " ;
cin >> answer;
getline(cin,temp);
inside_days();
}
void inside_days() {
if (answer == "calendar") {
insert_calendar_days();
}
else if (answer == "quit") {
exit(EXIT_SUCCESS);
}
else {
cout << "Please enter calendar or quit" << endl;
}
}
void insert_task_sunday() {
string sunday_task;
int sunday_time;
ofstream insert_task_sunday("sunday_task.txt");
int sunday_size;
cout << "Enter the number of task to be enter into sunday: ";
cin >> sunday_size;
for (int i = 0; i < sunday_size; i++) {
cout << "Enter the task on sunday :" << endl;
cin >> sunday_task;
cout << "Time? ";
cin >> sunday_time;
insert_task_sunday << sunday_task << ' ' << sunday_time << endl;
}
insert_task_sunday.close();
}
void print_task_sunday(( {
ifstream insert_task_sunday;
insert_task_sunday.open("sunday_task.txt");
while (true) {
string sunday_task;
int sunday_time;
insert_task_sunday >> sunday_task >> sunday_time;
if (insert_task_sunday.eof())break ;
cout.width(6); cout << left << sunday_task << ' ' << sunday_time << endl;
}
insert_task_sunday.close();
}
关于随机数...
这些不是随机数,而是包含两个空格的字符常量的值。
您正在输出包含两个空格的字符文本' '
而不是具有 1 个空格' '
的字符文本或具有两个空格" "
的字符串。
常量中的多个字符是实现定义的行为。
包含多个整数字符常量的值 字符或包含不 映射到单字节执行字符(C90 6.1.3.4、C99 和 C11( 6.4.4.4(. 请参阅实现定义的行为。
完整重现:
#include <iostream>
int main() {
std::cout << ' ';
}
输出(Visual Studio(:
8224
至于你的其他问题...
这段代码(如下(对我有用。
#include <string>
#include <fstream>
#include <iostream>
using namespace std;
int main() {
string sunday_task = "sundaytask1";
int sunday_time = 930;
string monday_task = "mondaytask1";
int monday_time = 930;
string tuesday_task = "tuesdaytask1";
int tuesday_time = 930;
string wednesday_task = "wednesdaytask1";
int wednesday_time = 930;
string thursday_task = "thursdaytask1";
int thursday_time = 930;
string friday_task = "fridaytask1";
int friday_time = 930;
string saturday_task = "saturdaytask1";
int saturday_time = 930;
ifstream file1("sunday_task.txt", ios::in);
ifstream file2("monday_task.txt", ios::in);
ifstream file3("tuesday_task.txt", ios::in);
ifstream file4("wednesday_task.txt", ios::in);
ifstream file5("thursday_task.txt", ios::in);
ifstream file6("friday_task.txt", ios::in);
ifstream file7("saturday_task.txt", ios::in);
ofstream ofile("all_task.txt", ios::out | ios::app);
ofile << file1.rdbuf() << file2.rdbuf() << file3.rdbuf() <<
file4.rdbuf() << file5.rdbuf() << file6.rdbuf() << file7.rdbuf();
cout << sunday_task << ' ' << sunday_time << ' ' << monday_task << ' ' <<
monday_time << ' ' << tuesday_task << ' ' << tuesday_time << ' ' <<
wednesday_task << ' ' << wednesday_time << ' ' << thursday_task << ' ' <<
thursday_time << ' ' << friday_task << ' ' << friday_time << ' ' <<
saturday_task << ' ' << saturday_time << endl;
}