jj_ Skrevet 30. april 2008 Del Skrevet 30. april 2008 (endret) Prøver å skrive ut en string med cout, men får feilmeldingen: error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) på denne linja: cout << sDisplay; Jeg karer ikke å finne hva som er feil, er det noen som kan hjelpe meg? Bruker Visual Studio Express 2008. Resten av koden er som følger (linja med feil er nest nederste linje): // DateIO.cpp // // Program to recieve, validate and display a date. // // Original Code: M.Mason // Modified by: <<insert your name here >> // #include <iostream> #include <iomanip> using namespace std; // Function Prototypes bool GetDate(int &nDay, int &nMonth, int &nYear); bool ValidDate(int nDay, int nMonth, int nYear); bool DisplayDate(int nDay, int nMonth, int nYear); // Main int main(int argc, char *argv[]){ int nFirstDay; int nFirstMonth; int nFirstYear; do { GetDate(nFirstDay, nFirstMonth, nFirstYear); } while ( !ValidDate(nFirstDay, nFirstMonth, nFirstYear) ); DisplayDate(nFirstDay, nFirstMonth, nFirstYear); return 0; } // Function implementations ... // bool GetDate(int &nDay, int &nMonth, int &nYear) // prompts user for the day, the month and the year (seperately) // checks to see that it got numbers. bool GetDate(int &nDay, int &nMonth, int &nYear){ do{ if (cin.fail()){ cin.clear(); while (cin.get() != '\n'); cout << "An error occured: Expected integers" << '\n'; } cout << "Enter date (dd mm yyyy): "; cin >> nDay >> nMonth >> nYear; } while (!cin.good()); return true; } // bool ValidDate(int nDay, int nMonth, int nYear) // Checks the values of nDay, nMonth and nYear to see if they form a valid date. // returns false if the year is invalid. bool ValidDate(int nDay, int nMonth, int nYear){ string sDay = "" + nDay; string sMonth = "" + nMonth; string sYear = "" + nYear; bool bErr = false; if (nDay < 1 || nDay > 31) bErr = true; if (nMonth < 1 || nMonth > 12) bErr = true; if (nYear < 1900 || nYear > 2008) bErr = true; if (bErr){ cout << "\nError in date format, please try again\n"; return false; } else { return true; } } // bool DisplayDate(int nDay, int nMonth, int nYear) // Writes the date out tot he cout stream in DD/MM/YYYY format bool DisplayDate(int nDay, int nMonth, int nYear){ string sDisplay; enum months{ jan = 1, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec}; switch (nMonth){ case jan: sDisplay = "January "; break; case feb: sDisplay = "February "; break; case mar: sDisplay = "March "; break; case apr: sDisplay = "April "; break; case may: sDisplay = "May "; break; case jun: sDisplay = "June "; break; case jul: sDisplay = "July "; break; case aug: sDisplay = "August "; break; case sep: sDisplay = "September "; break; case oct: sDisplay = "October "; break; case nov: sDisplay = "November "; break; case dec: sDisplay = "December "; break; } if (nDay == 1){ sDisplay += nDay + "st "; } else if (nDay == 2){ sDisplay += nDay + "nd "; } else if (nDay == 3){ sDisplay += nDay + "rd "; } else { sDisplay += nDay + "th "; } sDisplay += nYear; cout << sDisplay; return true; } Endret 30. april 2008 av jj_ Lenke til kommentar
jj_ Skrevet 30. april 2008 Forfatter Del Skrevet 30. april 2008 Tja, ser ut som jeg måtte ha med #include <sstream> , nå virker det i allefall: // DateIO.cpp // // Program to recieve, validate and display a date. // // Original Code: M.Mason // Modified by: <<insert your name here >> // #include <iostream> #include <iomanip> #include <sstream> using namespace std; // Function Prototypes bool GetDate(int &nDay, int &nMonth, int &nYear); bool ValidDate(int nDay, int nMonth, int nYear); bool DisplayDate(int nDay, int nMonth, int nYear); // Main int main(int argc, char *argv[]){ int nFirstDay; int nFirstMonth; int nFirstYear; do { GetDate(nFirstDay, nFirstMonth, nFirstYear); } while ( !ValidDate(nFirstDay, nFirstMonth, nFirstYear) ); DisplayDate(nFirstDay, nFirstMonth, nFirstYear); return 0; } // Function implementations ... // bool GetDate(int &nDay, int &nMonth, int &nYear) // prompts user for the day, the month and the year (seperately) // checks to see that it got numbers. bool GetDate(int &nDay, int &nMonth, int &nYear){ do{ if (cin.fail()){ cin.clear(); while (cin.get() != '\n'); cout << "\nAn error occured: Expected integers\n"; } cout << "Enter date (dd mm yyyy): "; cin >> nDay >> nMonth >> nYear; } while (!cin.good()); return true; } // bool ValidDate(int nDay, int nMonth, int nYear) // Checks the values of nDay, nMonth and nYear to see if they form a valid date. // returns false if the year is invalid. bool ValidDate(int nDay, int nMonth, int nYear){ string sDay = "" + nDay; string sMonth = "" + nMonth; string sYear = "" + nYear; bool bErr = false; if (nDay < 1 || nDay > 31) bErr = true; if (nMonth < 1 || nMonth > 12) bErr = true; if (nYear < 1900 || nYear > 2008) bErr = true; if (bErr){ cout << "\nError in date format, please try again\n"; return false; } else { return true; } } // bool DisplayDate(int nDay, int nMonth, int nYear) // Writes the date out to the cout stream in MM DD YYYY format bool DisplayDate(int nDay, int nMonth, int nYear){ string sMonth; string sDay; string temp; stringstream convert; enum months{ jan = 1, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec}; switch (nMonth){ case jan: sMonth = "January "; break; case feb: sMonth = "February "; break; case mar: sMonth = "March "; break; case apr: sMonth = "April "; break; case may: sMonth = "May "; break; case jun: sMonth = "June "; break; case jul: sMonth = "July "; break; case aug: sMonth = "August "; break; case sep: sMonth = "September "; break; case oct: sMonth = "October "; break; case nov: sMonth = "November "; break; case dec: sMonth = "December "; break; } //set temp = nDay convert << nDay; temp = convert.str(); if(nDay > 20){ temp = temp.substr(1,1); } if (temp == "1"){ sDay = "st "; } else if (temp == "2"){ sDay = "nd "; } else if (temp == "3"){ sDay = "rd "; } else { sDay = "th "; } cout << sMonth << nDay << sDay << nYear << endl; return true; } Lenke til kommentar
Anbefalte innlegg
Opprett en konto eller logg inn for å kommentere
Du må være et medlem for å kunne skrive en kommentar
Opprett konto
Det er enkelt å melde seg inn for å starte en ny konto!
Start en kontoLogg inn
Har du allerede en konto? Logg inn her.
Logg inn nå