转换日期格式?

时间:2015-10-16 06:04:50

标签: c++

#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;
}

所以我在这里尝试做的是让用户输入ex:2012年3月22日星期二,然后有2个结果出来。第1个是“1月1日是2012年的星期二”,这很好。问题在于第二个结果我想要“2012年3月22日星期二”,但问题是在第46-53行,所有字符串都连接到第一个std:string str = dayow;所以输出变成了TueTuTuTues! 谁能帮忙?谢谢!

编辑:对不起,如果这是一个菜鸟问题:/真的很新编码!

3 个答案:

答案 0 :(得分:1)

您使用哪种编译器?你的程序不应该编译。

  1. 添加#include <iostream>
  2. 您无法定义具有相同名称C ++的少数变量。 所以替换

    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);
    

答案 1 :(得分:1)

试试这个:( 代码的某些更改

#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;
}

答案 2 :(得分:0)

更改此部分:

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 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);

问题是你有一天,一个月和一年的变量名称