Jump to content

Send help


Sidewalk Surfboard
 Share

Recommended Posts

Ok, new error popped up. This time I have my code. The error is that every time I press a key to continue, it just looks "insert a valid month" and "press a key to continue" forever.

#include <iostream>
#include <string>
using namespace std;

int main(){
    int month;
    start:
    cout <<"Birth Gem Database";
    cout << endl;
    cout <<"Please enter your birth month (number): ";
    cin >> month;
    cout << endl;
    if(month==1){
        cout <<"Your birth gem is Garnet.";
    }
    if(month==2){
        cout <<"Your birth gem is Amethyst.";
    }
    if(month==3){                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
        cout <<"Your birth gem is Aquamarine.";
    }
    if(month==4){
        cout <<"Your birth gem is Diamond.";
    }
    if(month==5){
        cout <<"Your birth gem is Emerald.";
    }
    if(month==6){
        cout <<"Your birth gem is Alexandrite.";
    }
    if(month==7){
        cout <<"Your birth gem is Ruby.";
    }
    if(month==8){
        cout <<"Your birth gem is Peridot.";
    }
    if(month==9){
        cout <<"Your birth gem is Sapphire.";
    }
    if(month==10){
        cout <<"Your birth gem is Tourmaline.";
    }
    if(month==11){
        cout <<"Your birth gem is Citrine.";
    }
    if(month==12){
        cout <<"Your birth gem is Blue Zircon.";
    cout << endl;
    system ("pause");
    return 0;
    }
    else{
        cout << "Insert a valid month.";
        cout << endl;
        system ("pause");
        cout << endl;
        goto start;
    }
    return 0;
}
   

Link to comment
Share on other sites

You need "else if" keywords to join the tests together, but honestly with that many choices to test a single variable for, you'd do a world better for readability to use a switch statement.  Example:

switch (x) {
  case 1:
    cout << "x is 1";
    break;
  case 2:
    cout << "x is 2";
    break;
  default:
    cout << "value of x unknown";
  }
Link to comment
Share on other sites

Here's some working code for you:

#include <iostream>
#include <string>
using namespace std;
int main()
{
    int month;
    start:
    cout <<"Birth Gem Database"<<endl;
    cout <<"Please enter your birth month (number): ";
    cin >> month;
    cout << endl;
    if(month==1) cout <<"Your birth gem is Garnet.";
    else if(month==2) cout <<"Your birth gem is Amethyst.";
    else if(month==3) cout <<"Your birth gem is Aquamarine.";
    else if(month==4) cout <<"Your birth gem is Diamond.";
    else if(month==5) cout <<"Your birth gem is Emerald.";
    else if(month==6) cout <<"Your birth gem is Alexandrite.";
    else if(month==7) cout <<"Your birth gem is Ruby.";
    else if(month==8) cout <<"Your birth gem is Peridot.";
    else if(month==9) cout <<"Your birth gem is Sapphire.";
    else if(month==10) cout <<"Your birth gem is Tourmaline.";
    else if(month==11) cout <<"Your birth gem is Citrine.";
    else if(month==12) cout <<"Your birth gem is Blue Zircon.";
	else {
		cout << "Insert a valid month.";
        cout << endl;
        system ("pause");
        cout << endl;
        goto start;
	}
	cout << endl;
	system ("pause");
	return 0;
}

Your issue was how you did the if statements. For example, if you pick 8 for the month, it goes through and checks each value. Once it checks with the right one, it prints out the text, then continues checking with the others. It checks with 12, fails, then executes the code in the else statement, which restarts your program.
Also, don't use goto with C++ code. Use a loop and then break out of it using break.
Another thing, you don't need braces after if when you're only going to do a single command.
Lastly, you could use switch for checking the months, like this:

switch(month){
case 1:
    cout <<"Your birth gem is Garnet.";
    break;
case 2:
    cout <<"Your birth gem is Amethyst.";
    break;
case 3:
    cout <<"Your birth gem is Aquamarine.";
    break;
case 4:
    cout <<"Your birth gem is Diamond.";
    break;
case 5:
    cout <<"Your birth gem is Emerald.";
    break;
case 6:
    cout <<"Your birth gem is Alexandrite.";
    break;
case 7:
    cout <<"Your birth gem is Ruby.";
    break;
case 8:
    cout <<"Your birth gem is Peridot.";
    break;
case 9:
    cout <<"Your birth gem is Sapphire.";
    break;
case 10:
    cout <<"Your birth gem is Tourmaline.";
    break;
case 11:
    cout <<"Your birth gem is Citrine.";
    break;
case 12:
    cout <<"Your birth gem is Blue Zircon.";
    break;
default:
    cout << "Insert a valid month.";
    cout << endl;
    system ("pause");
    cout << endl;
    goto start;
}

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...