How can i do a statement?

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

Was this solution helpful to you?

Find solution

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.