Can anyone look at my code and explain what I could be doing wrong with this if else statement. No matter how?
-
Can anyone look at my code and explain what I could be doing wrong with this if else statement. No matter how I configure the order I continue to get an error telling my that I have an If without an else statement.I have tried doing it outside a while loop and the incrementation is also not working for me. Also is there anyway to perform an If statement with a string variable and if so how would I need to grab the user's input value and run it through a control statement. Thanks in advance for any help. #include <iostream> #include <string> using namespace std; class Date { public: void setPayDate(string name) { userPayDate = name; } string getPayDate() { return userPayDate; } private: string userPayDate; };//end class Date int main() { using namespace std;//Using Namespace library int userIntChoice = 0; int loop1 = 1;//declaration variable for loop Date myDate1;//Creating object from Date class string userInput = "No Pay";//Default value for user input cout << "Do you get paid (1)weekly or (2)biweekly? "; cin >> userIntChoice; /* while(loop1 <= 2)// loop condition loop1 = loop1++;//loop timer { */ if ( userIntChoice == 1 ) userInput = "weekly"; myDate1.setPayDate(userInput); cout << "You receive a paycheck on a " << myDate1.getPayDate()<< " period!" << endl; else if( userIntChoice == 2 ) userInput = "biweekly"; myDate1.setPayDate(userInput); cout << "You receive a paycheck on a " << myDate1.getPayDate()<< " period!" << endl; else cout<<"You have entered an incorrect date for pay"<< endl; /* } */ myDate1.setPayDate(userInput); system("pause");
-
Answer:
You forgot the braces. if ( userIntChoice == 1 ) { //<<<<<<<<<<<<<<<<<<<<<<<<<< open brace here userInput = "weekly"; myDate1.setPayDate(userInput); cout << "You receive a paycheck on a " << myDate1.getPayDate()<< " period!" << endl; } //<<<<<<<<<<<<<<<<<<<<<<<<<<close brace here else if( userIntChoice == 2 ) { //<<<<<<<<<<<<<<<<<<<<<<<<,open brace here userInput = "biweekly"; myDate1.setPayDate(userInput); cout << "You receive a paycheck on a " << myDate1.getPayDate()<< " period!" << endl; } //<<<<<<<<<<<<<<<<<<<<close brace here else cout<<"You have entered an incorrect date for pay"<< endl; whenever you put more than one line of code inside an "if" statement, you always need to put the open and closing brace. This applies to "else if" and "else" as well as loops and any other code blocks.
Wes D at Yahoo! Answers Visit the source
Related Q & A:
- Can anyone explain this code to me?Best solution by Stack Overflow
- Can anyone give me the code for toshiba tv with the URC22B?Best solution by Yahoo! Answers
- Can anyone give me an idea about what i can doing in the holiday?traveling to Brazil or Paris or London?Best solution by Yahoo! Answers
- Could anyone please tell me what i can do to speed up my yahoo mail it used to be fast but now slow?Best solution by Yahoo! Answers
- Can anyone help with a code for a ford fiesta radio?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.