Error Building and Running

Feb 24, 2012 at 2:41pm
Hello there, i have recently started messing around with some C++ tutorials and currently using Dev-C++ to complete every action that i need to try, please note that its my very first experience at coding and im having a hard time starting off :( , i have joined this forum hoping that i will find people that wont mind helping me out a little, from what i read from my tutorials, i've understood some basic commands, and i just thought that i'd try this out, (which turns out not to work) , i was hoping you guys could tell me, If my code is absolutely wrong, or maybe it just has a mistake or two, please understand that i need this kind of method for my improvement, as simply reading tutorials isnt helping me :(

Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
using namespace std;

int main()
{
    int x,y
          
    cout << "Write a number:" << endl;
    cin >> x ;
    cout <<"Write another number" << endl;
    cin >> y ;
    cout <<"Wanna know the summ of those two numbers?" << endl;
    if 
    {
         cin >> yes
    then
        cout << x + y << endl;
    else
        cin >> no
        cout <<" Ok. " << endl; 
    }
    
    cout <<"Press the enter key to exit";
    cin.ignore(cin.rdbuf()->in_avail() + 1);
    return 0;
}


Errors that im getting while trying to compile:

9 D:\PROGRAMMING SECTION\GameOver!\GameOver!.cpp expected init-declarator before "cout"

9 D:\PROGRAMMING SECTION\GameOver!\GameOver!.cpp expected `,' or `;' before "cout"

12 D:\PROGRAMMING SECTION\GameOver!\GameOver!.cpp `y' undeclared (first use this function)

15 D:\PROGRAMMING SECTION\GameOver!\GameOver!.cpp expected `(' before '{' token

I kept trying to figure out how or what to do to fix the errors, but ... i guess you can imagine where im standing :( , right in middle of a dense fog without knowing which way im headed. Enlighten me please :(
Feb 24, 2012 at 2:54pm
Missing semicolon on line 6, 15 and 19.
Last edited on Feb 24, 2012 at 2:54pm
Feb 24, 2012 at 3:12pm
Hmmm :) Thank you Peter, fixed those, and this is the current code now, but still with one error

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
using namespace std;

int main()

{
    int x,y;
          
    cout << "Write a number:" << endl;
    cin >> x ;
    cout <<"Write another number" << endl;
    cin >> y ;
    cout <<"Wanna know the summ of those two numbers?" << endl;
    if 
    { 
    
         cin >> yes;
    then
        cout << x + y << endl;
    else
        cin >> no;
        cout <<" Ok. " << endl; 
    }
    
    cout <<"Press the enter key to exit";
    cin.ignore(cin.rdbuf()->in_avail() + 1);
    return 0;
}


Errors While trying to Compile :

D:\PROGRAMMING SECTION\GameOver!\GameOver!.cpp In function `int main()':
15 D:\PROGRAMMING SECTION\GameOver!\GameOver!.cpp expected `(' before '{' token

Cant seem to know what to do at this point :( , i tried the ( before { but didnt work ...
Feb 24, 2012 at 3:17pm
closed account (zb0S216C)
The closing brace of if must appear before else. Also, what's then doing there? Furthermore, where's if's condition?

Wazzak
Last edited on Feb 24, 2012 at 3:18pm
Feb 24, 2012 at 5:37pm
Thank you Wazzak ;) , you made me struggle for quite awhile,and this is the solution i came up with to my problem.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>

using namespace std;

int main()

{
    int x,y;
    
    cout << "Write a number:" << endl;
    cin >> x ;
    cout <<"Write another number" << endl;
    cin >> y ;
    cout <<"Wanna know the summ of those two numbers?" << endl;
    string ms1;
    cin >> ms1;
    if(ms1=="yes"){cout << x+y << endl; }
    else if(ms1=="no"){cout << "Ok." << endl; }
    
    
    return 0;
 }


I continued reading further on another tutorial and found out the proper use of Strings, so now, when i ask if you want to know the summ of those two numbers whatever answer is written ,yes,or no, it gets stored in ms1 (mystring1) , than i checked if ms1=="yes" and continued the action that i needed , the summary of those two numbers , and if ms1=="no" you will get a simple answer of Ok.

PS. However, i still havent figured out how to prevent the screen from closing when i press enter, i am very curious to know if i can make it close with ESC key instead.
Last edited on Feb 24, 2012 at 6:30pm
Feb 24, 2012 at 7:57pm
closed account (zb0S216C)
By default, the console will immediately close after the processing of the last instruction. You can stop this by:

In Visual C++ Studio:

1
2
3
4
5
int main( )
{
    std::cin.ignore( std::numeric_limits< std::streamsize >::max( ), '\n' );
    return( 0 );
}

In MinGW:

1
2
3
4
5
int main( )
{
    std::cin.ignore( INT_MAX, '\n' );
    return( 0 );
}


Wazzak
Feb 24, 2012 at 10:28pm
i am using Dev-C++ , and this code is 'pausing' the immediate closure until i press a key to close it,

1
2
3
4
5
#include <iostream>

cin.clear();
cin.ignore(255, '\n');
cin.get();


the #include <iostream> as usual on the beginning, but the rest of the code, right before the return 0; command :)
Feb 24, 2012 at 11:37pm
closed account (zb0S216C)
Dev-C++ is old software, and seriously out-dated. I'd suggest that you upgrade to a newer IDE, such as Code::Blocks (MinGW) or Visual C++ Express 2010.

Wazzak
Feb 25, 2012 at 9:14am
Hmmm i shall look into it, thanks for your suggestion:)
Feb 25, 2012 at 2:30pm
@Framework: you are making look as it was a compiler extension. Both codes are standard c++ and should work with any compiler (provided that you included the proper headers).
Topic archived. No new replies allowed.