Hello, I just began doing c++ today, and i've done a program like this:

#include <iostream>
using namespace std;

int main()
{
    int num1;
    int num2;
    int valinta;
    int valinta2;
    
    cout<<"Haluatko pelata pelia? 1.Kylla 2.En\n";
    cin>>valinta;
    if(valinta == 1){
         cout<<"Paljonko on 3+2: ";
        cin>>num1;
        cin.ignore();
        if(num1 == 5){
             cout<<"Aivan oikein, mutta paljonko on 5*5: ";
             cin>>num2;
             cin.ignore();
             if(num2 == 25){
                cout<<"Profeetta. Uudestaan? 1. Kylla 2. Ei\n";
                cin>>valinta2;
               if(valinta2 == 1){
                   return valinta;
               }
            }
        }
        else if(valinta == 2){
        }

       else{
            cout<<"Oletko todella noin paska?\n";
       }
       cin.get();
   }
}

You can see theres "return valinta;", because in the last IF, if the user writes 1, it will go back to beginging. But I cant get it work. If someone could please tell me how to do it, I woulda be really happy.

When the person, whos "playing" that math game, finishes it, the game will ask if the player wants to play again, or quit. If the player chooses to play again, he will push 1, and the whole game will start again from the beginging.

In the last if, you need to change from

if(valinta2 == 1){
return valinta;
}

to

if(valinta2 == [B]2[/B]){
return valinta;
}

You can do with one variable for getting the user's choice i.e. use only valinta and toss away valinta2 altogether.
And also drop the needless else if block

else if(valinta == 2){
}

And last but not least, your program may leave the user with bad taste, in case he does not know the correct answer for the first question ...

You need to un-nest your if statements. You need to rethink your code some. What you need to do is take each individual process (input, calculation, output, etc) as individual steps. What you have is each step integrated (merged) into the previous step. Separate them into input, calculation, output, and so on. You will be able to expand the program easier.

Hello, I just began doing c++ today, and i've done a program like this:

#include <iostream>
using namespace std;

int main()
{
    int num1;
    int num2;
    int valinta;
    int valinta2;
    
    cout<<"Haluatko pelata pelia? 1.Kylla 2.En\n";
    cin>>valinta;
    if(valinta == 1){
         cout<<"Paljonko on 3+2: ";
        cin>>num1;
        cin.ignore();
        if(num1 == 5){
             cout<<"Aivan oikein, mutta paljonko on 5*5: ";
             cin>>num2;
             cin.ignore();
             if(num2 == 25){
                cout<<"Profeetta. Uudestaan? 1. Kylla 2. Ei\n";
                cin>>valinta2;
               if(valinta2 == 1){
                   return valinta;
               }
            }
        }
        else if(valinta == 2){
        }

       else{
            cout<<"Oletko todella noin paska?\n";
       }
       cin.get();
   }
}

You can see theres "return valinta;", because in the last IF, if the user writes 1, it will go back to beginging. But I cant get it work. If someone could please tell me how to do it, I woulda be really happy.

Hi there,
first please try to type every thing in English to be easy to understand

but wht i can see
that your code has a very small problem which is :

int main()
{
//your code


return 0;//the missing statement
}

as an Explanation you said that the return type is integer and finally you didn't make a return statement... main() is a function and you specified that it will return integer

-you can replace int with void and it will work in Microsoft visual C++ but this method won't work in some compilers like DJGPP as the main function can't be void it must return in such compilers.

-you can simply type the missing statement and your code will work

return 0;

yours,
Samer al daur

This article has been dead for over six months. Start a new discussion instead.