C++ 转换日期格式? #包括 #包括 #包括 #包括 使用名称空间std; //声明变量 字符串dayow; 弦月; 弦日; 弦年; int main() { 不能更改此部分: #include <iostream> #include <iomanip> #include <string> #include <fstream> using namespace std; // declares variables string dayow; string month; string day; string year; int main() { cout << "Pick you day of the week (ex: Monday-Sunday)" << endl; getline(cin, dayow); cout << " " << endl; cout << "Pick your month (ex: January-December)" << endl; getline(cin, month); cout << " " << endl; cout << "Pick your day of the month (ex: 1-31)" << endl; getline(cin, day); cout << " " << endl; cout << "Pick your year" << endl; getline(cin, year); cout << " " << endl; cout << "This is your date.." << endl; cout << dayow << ", " << month << " " << day << ", " << year << "." << endl; cout << " " << endl; cout << "Here are the 3 formats to display your date.." << endl; cout << " " << endl; cout << "1. " << month << " " << day << " was a " << dayow << " in " << year << endl; cout << " " << endl; std::string str = dayow; std::string str1 = str.substr(0, 3); std::string str = month; std::string str2 = str.substr(0, 2); std::string str = day; std::string str3 = str.substr(0, 2); std::string str = year; std::string str4 = str.substr(0, 4); std::cout << str1 << ", " << str2 << " " << str3 << " '" << str4 << endl; return 0; }
致:C++ 转换日期格式? #包括 #包括 #包括 #包括 使用名称空间std; //声明变量 字符串dayow; 弦月; 弦日; 弦年; int main() { 不能更改此部分: #include <iostream> #include <iomanip> #include <string> #include <fstream> using namespace std; // declares variables string dayow; string month; string day; string year; int main() { cout << "Pick you day of the week (ex: Monday-Sunday)" << endl; getline(cin, dayow); cout << " " << endl; cout << "Pick your month (ex: January-December)" << endl; getline(cin, month); cout << " " << endl; cout << "Pick your day of the month (ex: 1-31)" << endl; getline(cin, day); cout << " " << endl; cout << "Pick your year" << endl; getline(cin, year); cout << " " << endl; cout << "This is your date.." << endl; cout << dayow << ", " << month << " " << day << ", " << year << "." << endl; cout << " " << endl; cout << "Here are the 3 formats to display your date.." << endl; cout << " " << endl; cout << "1. " << month << " " << day << " was a " << dayow << " in " << year << endl; cout << " " << endl; std::string str = dayow; std::string str1 = str.substr(0, 3); std::string str = month; std::string str2 = str.substr(0, 2); std::string str = day; std::string str3 = str.substr(0, 2); std::string str = year; std::string str4 = str.substr(0, 4); std::cout << str1 << ", " << str2 << " " << str3 << " '" << str4 << endl; return 0; },c++,C++,致: 问题是您的日、月和年变量名相同,您使用哪种编译器?您的程序不应该编译 添加#包括 >你不能用相同的名字C++定义几个变量。 因此,替换 std::string str = dayow; std::string str1 = str.substr(0, 3); std::string stra = month; std::string str2 = stra.substr(0, 3); std::string strb = day; std::string str3 = strb.subs
问题是您的日、月和年变量名相同,您使用哪种编译器?您的程序不应该编译
#包括
std::string str = dayow;
std::string str1 = str.substr(0, 3);
std::string stra = month;
std::string str2 = stra.substr(0, 3);
std::string strb = day;
std::string str3 = strb.substr(0, 2);
std::string strc = year;
std::string str4 = strc.substr(0, 4);
与
试试这个:(对代码进行一些更改)
#包括
#include/**添加此头文件**
#包括
#包括
使用名称空间std;
//声明变量
字符串dayow;
弦月;
弦日;
弦年;
int main()
{
这不应该编译。你有多个str
的定义。你是如何从这个程序中获得输出的?你是我的英雄!你知道如何将2012年3月22日星期二的日期更改为类似2012-03-22的日期吗?只是出于好奇:这是你的家庭作业,提示:使用一个循环来分配月份编号并根据它设置输出格式:)
std::string str = dayow;
std::string str1 = str.substr(0, 3);
std::string stra = month;
std::string str2 = stra.substr(0, 3);
std::string strb = day;
std::string str3 = strb.substr(0, 2);
std::string strc = year;
std::string str4 = strc.substr(0, 4);
std::string str = dayow;
std::string str1 = str.substr(0, 3);
std::string str = month;
std::string str2 = str.substr(0, 2);
std::string str = day;
std::string str3 = str.substr(0, 2);
std::string str = year;
std::string str4 = str.substr(0, 4);
std::string str1 = dayow.substr(0, 3);
std::string str2 = month.substr(0, 2);
std::string str3 = day.substr(0, 2);
std::string str4 = year.substr(0, 4);
#include <iomanip>
#include<iostream> // **add this header file**
#include <string>
#include <fstream>
using namespace std;
// declares variables
string dayow;
string month;
string day;
string year;
int main()
{
cout << "Pick you day of the week (ex: Monday-Sunday)" << endl;
getline(cin, dayow);
cout << " " << endl;
cout << "Pick your month (ex: January-December)" << endl;
getline(cin, month);
cout << " " << endl;
cout << "Pick your day of the month (ex: 1-31)" << endl;
getline(cin, day);
cout << " " << endl;
cout << "Pick your year" << endl;
getline(cin, year);
cout << " " << endl;
cout << "This is your date.." << endl;
cout << dayow << ", " << month << " " << day << ", " << year << "." << endl;
cout << " " << endl;
cout << "Here are the 3 formats to display your date.." << endl;
cout << " " << endl;
cout << "1. " << month << " " << day << " was a " << dayow << " in " << year << endl;
cout << " " << endl;
// **changes start here**
string str1 = dayow.substr(0, 3);
string str2 = month.substr(0, 3);
string str3 = day.substr(0, 2);
string str4 = year.substr(0, 4);
cout << str1 << ", " << str2 << " " << str3 << " ," << str4 << endl;
return 0;
}