1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: C++ - Prefixes project...

Discussion in 'Software Development' started by Raistlfiren, Jan 26, 2006.

Thread Status:
Not open for further replies.
Advertisement
  1. Raistlfiren

    Raistlfiren Thread Starter

    Joined:
    Jul 13, 2004
    Messages:
    303
    Hey, Im trying to make a little project to learn a little more about C++ and a little more on Chemistry. This is the source code :

    #include <iostream>
    #include <string>
    #include <cstdlib>

    using namespace std;

    const int LOW = 2;
    const int HIGH = 11;

    string answer;
    int numbers;

    inline void Prefixes()
    {
    numbers = rand() % (HIGH - LOW);

    cout << "What is the correct prefix for " << numbers << " \?";
    cout << "\n";
    cin >> answer;
    }

    int main ()
    {

    Prefixes();

    if ( (numbers == 1) && (answer == "mono"))

    cout << "You answered with the correct answer!\n\n";
    else
    cout << "You answered with the wrong answer. Please try again!\n\n";


    if ( (numbers == 2) && (answer == "di"))

    cout << "You answered with the correct answer!\n\n";
    else
    cout << "You answered with the wrong answer. Please try again!\n\n";


    if ( (numbers == 3) && (answer == "tri"))

    cout << "You answered with the correct answer!\n\n";
    else
    cout << "You answered with the wrong answer. Please try again!\n\n";


    if ( (numbers == 4) && (answer == "tetra"))

    cout << "You answered with the correct answer!\n\n";
    else
    cout << "You answered with the wrong answer. Please try again!\n\n";



    if ( (numbers == 5) && (answer == "penta"))

    cout << "You answered with the correct answer!\n\n";
    else
    cout << "You answered with the wrong answer. Please try again!\n\n";



    if ( (numbers == 6) && (answer == "hexa"))

    cout << "You answered with the correct answer!\n\n";
    else
    cout << "You answered with the wrong answer. Please try again!\n\n";



    if ( (numbers == 7) && (answer == "hepta"))

    cout << "You answered with the correct answer!\n\n";
    else
    cout << "You answered with the wrong answer. Please try again!\n\n";




    if ( (numbers == 8) && (answer == "octa"))

    cout << "You answered with the correct answer!\n\n";
    else
    cout << "You answered with the wrong answer. Please try again!\n\n";



    if ( (numbers == 9) && (answer == "nona"))

    cout << "You answered with the correct answer!\n\n";
    else
    cout << "You answered with the wrong answer. Please try again!\n\n";



    if ( (numbers == 10) && (answer == "deca"))

    cout << "You answered with the correct answer!\n\n";
    else
    cout << "You answered with the wrong answer. Please try again!\n\n";

    return 0;

    }

    Really simple project for someone who knows what their doing, but my problem is that whenever I srun up the program it checks each if statement in the program! I want it to just check one statement, so if the random umbers == 1 then it will only check the statement that equals the prefic "mono" and wont go through each if statement.

    Thanks for any help,

    Rat
     
  2. cristobal03

    cristobal03

    Joined:
    Aug 5, 2005
    Messages:
    3,086
    You need to structure your logic just a bit more using something like a select case or nested conditionals. I don't know C++ but here's a model of the construct I'm suggesting:

    Code:
    Select Case numbers
      Case 1
        If answer == "mono" Then
          ' correct
        else
          ' incorrect
    
      Case 2
        If answer == "di" Then
          ' correct
        else
          ' incorrect
    
      Case 3
        If answer == "tri" Then
          ' correct
        else
          ' incorrect
    
    '...
    
      Case n
        If answer == "nroot" Then
          'correct
        Else
          'incorrect
    
    End Select
    
    You could do the same thing with nested If/ElseIf construction.

    HTH

    chris.
     
  3. Raistlfiren

    Raistlfiren Thread Starter

    Joined:
    Jul 13, 2004
    Messages:
    303
    Yes, but if I do that though, will the program know what random number is coming out? It randomly generates numbers from 1-10 and then numbers (which is the rand number generated) must be compared to the number. Can it read the random number without putting down "
    PHP:
    if (number==1then .... "
    I thought I would have to compare then random number to a real number, so I can then check to see if the answer is right.

    Rat
     
  4. cristobal03

    cristobal03

    Joined:
    Aug 5, 2005
    Messages:
    3,086
    Sorry I wasn't more clear. Your routine Prefixes would not need to change, nor do your variables. All that I suggested was changing how you process the return values. I can't be more specific because I don't know the programming language, but I can guarantee you the logic is the same. In fact, I could give you a solution using nested If...Then statements, but it will probably be less efficient than using a Select Case.

    It's important to understand the reason behind the problem; your construction was computing one statement at a time, but you didn't have any way of breaking out of the code block if a condition was met. So then, even if a condition was met, the code keeps testing the condition. Try this (I don't know if the correct syntax is elseif or elsif):

    Code:
    int main ()
    {
    
    Prefixes();
    
    if (numbers == 1) {
    if (answer == "mono") {
    
    cout << "You answered with the correct answer!\n\n";
    
    } else  {
    
    cout << "You answered with the wrong answer. Please try again!\n\n";
    
    } elsif  (numbers == 2) {
    if (answer == "di") {
    
    cout << "You answered with the correct answer!\n\n";
    
    } else  {
    
    cout << "You answered with the wrong answer. Please try again!\n\n";
    
    } elsif  (numbers == 3) {
    if (answer == "tri") {
    
    cout << "You answered with the correct answer!\n\n";
    
    } else {
    
    cout << "You answered with the wrong answer. Please try again!\n\n";
    
    } elsif '...
    
    } elsif  (numbers == 10) {
    if (answer == "deca") {
    
    cout << "You answered with the correct answer!\n\n";
    
    } else {
    
    cout << "You answered with the wrong answer. Please try again!\n\n";
    
    }
    }
    
    return 0;
    
    }
    
    See the difference? If the number isn't 1, then it goes on to the next condition (number == 2?). Otherwise, it goes into the nested condition. When the nested condition completes, because the parent condition was fulfilled, the code execution moves to the first line following the entire conditional block.

    A Select Case is a similar construct.

    HTH

    chris.
     
  5. Raistlfiren

    Raistlfiren Thread Starter

    Joined:
    Jul 13, 2004
    Messages:
    303
    Thanks a lot. I got it running bettter! I believe I tryed your idea out actually before with the else if statements, but I came up with multiple errors do to the fact that after the else if statements I didn't follow that by an if statement which produced multiple errors and the if statement produced none at the time .

    Thanks a lot,

    Rat
     
  6. cristobal03

    cristobal03

    Joined:
    Aug 5, 2005
    Messages:
    3,086
    No problem (y)

    You can mark this thread Solved using the Thread Tools at the top of the page, if you like.

    chris.
     
  7. InterKnight

    InterKnight

    Joined:
    Oct 18, 2004
    Messages:
    355
    Hello there, Raistlfiren.

    I was going to post yesterday, but I did not have much time.

    I'm assuming you got it working with if/else if statements. There is (I believe) another way to go about this as well. What you could do is something like:
    Code:
    switch(numbers)
    {
    case 1:
         if(answer == "mono")
              cout << "Correct answer...\n";
    break;
    case 2:
         ...
    break;
    ...
    }//end switch
    
    If you wanted the user to keep trying to get the correct answer, you could (should be able to) do something like this:
    Code:
    switch(numbers)
    {
    case 1:
         while(answer != "mono")
         {
              cout << "Incorrect answer.\nPlease try again: ";
              cin >> answer;
              cout << endl;
         }
         
         cout << "Correct answer...\n";
    break;
    ...
    }//end switch
    
    The switch would then only run the scenario of the random number that was assigned to the "numbers" variable.

    So the if/else if and the switch would be two ways of going about this.

    Hope this has been of some help.

    Take care.
     
  8. cristobal03

    cristobal03

    Joined:
    Aug 5, 2005
    Messages:
    3,086
    Ah, so switch is the C++ syntax for a case structure. (y)

    chris.
     
  9. InterKnight

    InterKnight

    Joined:
    Oct 18, 2004
    Messages:
    355
    Yep.

    I thought that was what you were referring to in your post when I saw it.

    I can't remember what it was in Visual Basic as I have not used VB for years...Prefer C++ myself...but each language has its different ways of doing things.

    In a switch the "break;" statement stops the program from progressing to the next level case...(that is what my professor said in class, anyhow).

    The switch() definitely makes things shorter; I used to use if/else if, and still do for some programs, but for many programs the switch() statment shortens things a bit.

    Take care.
     
  10. Raistlfiren

    Raistlfiren Thread Starter

    Joined:
    Jul 13, 2004
    Messages:
    303
    w00t! Thanks, I will check that out!

    Rat
     
  11. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/437491

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice