| | |
Hello Everyone, I need Help with my C++ Code...
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Dec 2004
Posts: 5
Reputation:
Solved Threads: 0
Hello People,
I need help because i just need to figure out whats wrong with the code i wrote for my program.. i tried to figure it out but i'm stuck. So here it is and i would appreciate it if someone can set me strait. Thanx alot.
Well here it is..
I am writing a program that computes the number of days between two dates.
It gives me a couple of error messages.
I don't know what i'm not using write or what i'm doing wrong!!
Please help me... Thanx again.
I need help because i just need to figure out whats wrong with the code i wrote for my program.. i tried to figure it out but i'm stuck. So here it is and i would appreciate it if someone can set me strait. Thanx alot.
Well here it is..
I am writing a program that computes the number of days between two dates.
It gives me a couple of error messages.
I don't know what i'm not using write or what i'm doing wrong!!
Please help me... Thanx again.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <iomanip> #include <string> #include <cctype> #include <cstring> using namespace std; int main(int) { const int YEAR= 365; int i; string inMonth1; int inMonth1Int; string inDay1; int inDay1Int; string inYear1; int inYear1Int; string inEra1; string inMonth2; int inMonth2Int; string inDay2; int inDay2Int; string inYear2; int inYear2Int; string inEra2; char inMonth1Char; char inMonth2Char; int date1; int date2; bool badInput; long double in1; long double in2; while (badInput==false) { while (inMonth1!="quit" &&inDay1!="quit" &&inYear1!="quit" && inEra1!="quit" &&inMonth2!="quit" &&inDay2!="quit" &&inYear2!="quit" && inEra2!="quit") { cout << "This program calculates the number of days between two dates."<< endl; cout << "Please enter the dates using only the following format:"<< endl; cout << "Month's abreviation, day, year, BC or AD" << endl; cout << "Then press enter, and the next date"<< endl; cout << "Example:"<< endl << "Jun 29 2500 BC (Enter)"<< endl<<"Jun 29 1238 AD (Enter)" << endl; cout << "To quit, type the word 'quit' and enter."<< endl; cout << "For the year of Jesus' death exactly, please type 0 for the year." << endl; cout << "We're sorry, no other format can be accepted."<< endl; cin.ignore('\n') >> inMonth1 >> inDay1 >> inYear1 >> inEra1 >> inMonth2 >> inDay2 >> inYear2 >> inEra2; i=inMonth1.length(); while (i>0) { inMonth1[i]=tolower(inMonth1[i]); i--; } i=0; i=inMonth2.length(); while (i>0) { inMonth2[i]=tolower(inMonth2[i]); i--; } if(inMonth1!="jan"&&inMonth1!="feb"&&inMonth1!="mar"&&inMonth1!="apr"&&inMonth1!="may"&&inMonth1!="jun"&&inMonth1!="jul"&&inMonth1!="aug"&&inMonth1!="sep"&&inMonth1!="oct"&&inMonth1!="nov"&&inMonth1!="dec") badInput=true; if (inMonth1=="jan") inMonth1Char='a'; if (inMonth1=="feb") inMonth1Char='b'; if (inMonth1=="mar") inMonth1Char='c'; if (inMonth1=="apr") inMonth1Char='d'; if (inMonth1=="may") inMonth1Char='e'; if (inMonth1=="jun") inMonth1Char='f'; if (inMonth1=="jul") inMonth1Char='g'; if (inMonth1=="aug") inMonth1Char='h'; if (inMonth1=="sep") inMonth1Char='i'; if (inMonth1=="oct") inMonth1Char='j'; if (inMonth1=="nov") inMonth1Char='k'; if (inMonth1=="dec") inMonth1Char='l'; inDay1Int=atoi(inDay1); inYear1Int=atoi(inYear1); inDay2Int=atoi(inDay2); inYear2Int=atoi(inYear2); switch (inMonth1Char) { case 'a': if(inDay1Int>0&&inDay1Int<32) { inMonth1Int=31; break; } else badInput=true; case 'b': if(inYear1Int%4==0) { if(inDay1Int>0&&inDay1Int<30) { inMonth1Int=29; break; } else badInput=true; } else { if(inDay1Int>0&&inDay1Int<29) { inMonth1Int=28; break; } else badInput=true; } case 'c': if(inDay1Int>0&&inDay1Int<32) { inMonth1Int=31; break; } else badInput=true; case 'd': if(inDay1Int>0&&inDay1Int<31) { inMonth1Int=30; break; } else badInput=true; case 'e': if(inDay1Int>0&&inDay1Int<32) { inMonth1Int=31; break; } else badInput=true; case 'f': if(inDay1Int>0&&inDay1Int<31) { inMonth1Int=30; break; } else badInput=true; case 'g': if(inDay1Int>0&&inDay1Int<32) { inMonth1Int=31; break; } else badInput=true; case 'h': if(inDay1Int>0&&inDay1Int<32) { inMonth1Int=31; break; } else badInput=true; case 'i': if(inDay1Int>0&&inDay1Int) { inMonth1Int=30; break; } else badInput=true; case 'j': if(inDay1Int>0&&inDay1Int<32) { inMonth1Int=31; break; } else badInput=true; case 'k': if(inDay1Int>0&&inDay1Int<31) { inMonth1Int=30; break; } else badInput=true; case 'l': if(inDay1Int>0&&inDay1Int<32) { inMonth1Int=31; break; } else badInput=true; badInput=true; } switch (inMonth2Char) { case 'a': if(inDay2Int>0&&inDay2Int<32) { inMonth2Int=31; break; } else badInput=true; case 'b': if(inYear2Int%4==0) { if(inDay2Int>0&&inDay2Int<30) { inMonth2Int=29; break; } else badInput=true; } else { if(inDay2Int>0&&inDay2Int<29) { inMonth2Int=28; break; } else badInput=true; } case 'c': if(inDay2Int>0&&inDay2Int<32) { inMonth2Int=31; break; } else badInput=true; case 'd': if(inDay2Int>0&&inDay2Int<31) { inMonth2Int=30; break; } else badInput=true; case 'e': if(inDay2Int>0&&inDay2Int<32) { inMonth2Int=31; break; } else badInput=true; case 'f': if(inDay2Int>0&&inDay2Int<31) { inMonth2Int=30; break; } else badInput=true; case 'g': if(inDay2Int>0&&inDay2Int<32) { inMonth2Int=31; break; } else badInput=true; case 'h': if(inDay2Int>0&&inDay2Int<32) { inMonth2Int=31; break; } else badInput=true; case 'i': if(inDay2Int>0&&inDay2Int) { inMonth2Int=30; break; } else badInput=true; case 'j': if(inDay2Int>0&&inDay2Int<32) { inMonth2Int=31; break; } else badInput=true; case 'k': if(inDay2Int>0&&inDay2Int<31) { inMonth2Int=30; break; } else badInput=true; case 'l': if(inDay2Int>0&&inDay2Int<32) { inMonth2Int=31; break; } else badInput=true; badInput=true; } if (inEra1!="AD"&&inEra1!="BC") badInput=true; if (inEra1=="AD") { in1=(inYear1Int*365.24219)+inDay1Int+inMonth1Int; } if (inEra1=="BC") { in1=(inYear1Int*365.24219)+inDay1Int+inMonth1Int*(0-1); } } } return 0; }
Last edited by alc6379; Dec 6th, 2004 at 11:34 am. Reason: added [code] tags
split it up into smaller, easier to comprehend chunks (iow, use what you learned about methods, classes, etc.).
That should unstick you probably.
If not think of how you'd do it without a computer. Write that down, then translate it into computer code one bit at a time (again, taking care to split it up into small chunks you can comprehend as a single entity).
That should unstick you probably.
If not think of how you'd do it without a computer. Write that down, then translate it into computer code one bit at a time (again, taking care to split it up into small chunks you can comprehend as a single entity).
•
•
Join Date: Dec 2004
Posts: 13
Reputation:
Solved Threads: 1
atoi requires const char*as an argument. string is a specialization of basic_string and is not a const char*. Call method c_str() on the string to get const char*. string owns the memory for the char buffer, so make sure your string goes out of scope after you have finished with the cons char*. The lines
inDay1Int=atoi(inDay1);
inYear1Int=atoi(inYear1);
inDay2Int=atoi(inDay2);
inYear2Int=atoi(inYear2);
should be
inDay1Int=atoi(inDay1.c_str());
inYear1Int=atoi(inYear1.c_Str());
inDay2Int=atoi(inDay2.c_str());
inYear2Int=atoi(inYear2.c_str());
Also, variable inMonth2Char is used bit not set anywhere.
inDay1Int=atoi(inDay1);
inYear1Int=atoi(inYear1);
inDay2Int=atoi(inDay2);
inYear2Int=atoi(inYear2);
should be
inDay1Int=atoi(inDay1.c_str());
inYear1Int=atoi(inYear1.c_Str());
inDay2Int=atoi(inDay2.c_str());
inYear2Int=atoi(inYear2.c_str());
Also, variable inMonth2Char is used bit not set anywhere.
![]() |
Similar Threads
- Code Snippet: Sample code for taking user input from Shell (Java)
- Code Snippet: code to split (and reconnect) an Access database (part 1) (Visual Basic 4 / 5 / 6)
- Code Snippet: Code that stores and retrieves. (C)
- Code Snippet: Efficient code for extracting unique elements from sorted array. (C)
- Code Snippet: Strings: Looking at ASCII Code (C)
- Code Snippet: Morse Code (C)
- Code Snippet: Gray Code Conversion (C)
- Code Snippet: code to validate data in Access withOUT using messageboxes (Visual Basic 4 / 5 / 6)
- Code Snippet: code to split (and reconnect) an Access database (part 2) (Visual Basic 4 / 5 / 6)
- Code Snippet: Send HTML code to Internet Explorer from basic (Legacy and Other Languages)
Other Threads in the C++ Forum
- Previous Thread: OnInitUpdate
- Next Thread: une tiny help plz!!!
Views: 2135 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays based beginner binary c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting spoonfeeding string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






