I have a problem...

This is a discussion on I have a problem... within the C++ Programming forums, part of the General Programming Boards category; I was going to make a calculator, and whaddayaknow, it didn't work. Whatever I do, I can't seem to fix ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    6

    Question I have a problem...

    I was going to make a calculator, and whaddayaknow, it didn't work. Whatever I do, I can't seem to fix it.

    Here comes the source code...
    Code:
    [tag]#include <iostream>
    
    using namespace std;
    
    int main()
    {
        int a;
    
        cout<<"Please enter a number up to a maximum of 10 billions: ";
        cin>>a;
    
        int b;
    
        cout<<"Please enter another number up to a maximum of 10 billions: ";
        cin>>b;
    
        char c;
    
        cout<<"What would you like to do with the numbers? ";
        cin>>c;
    
        if c == +
    
        int d = a + b;
    
        if c == -
    
        int d = a - b;
    
        if c == *
    
        int d = a * b;
    
        cout<<"The answer is... "<< d <<"\n";
        cin>>d;
    }[/tag]
    And here are the problems...
    Code:
    [tag] error: expected '(' before "c"
     error: expected '(' before "c"
     error: expected '(' before "c"
     error: 'd' was not declared in this scope[/tag]
    So, um... Help?

    PS. I have only done the first 2 tutorials, so I am aware that I am a newbie, and that you probably think this is the easiest things in the world.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    I laugh at your problem! j/k
    Actually, the problem is that fail to understand if statement syntax. Basic form is:
    if (expr)

    Also beware that any variable declared inside the if statements - ie
    Code:
    if (expr)
    {
        int x;
    }
    ...is not visible outside the if statement, so any variables you want to assign inside the if statement and use outside, you must define outside the if.

    Also, when comparing to a character, it must enclosed in quotes, ie '*'.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    6
    So I should do this? I mentioned i was a newbie, didn't I?

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int a;
    
        cout<<"Please enter a number up to a maximum of 10 billions: ";
        cin>>a;
    
        int b;
    
        cout<<"Please enter another number up to a maximum of 10 billions: ";
        cin>>b;
    
        char c;
    
        cout<<"What would you like to do with the numbers? ";
        cin>>c;
    
        if (c == +)
    {
        int d = a + b;
    }
        if (c == -)
    {
        int d = a - b;
    }
        if (c == *)
    {
        int d = a * b;
    }
        cout<<"The answer is... "<< 'd' <<"\n";
        cin>>d;
    }
    Last edited by Arcantos247; 11-28-2009 at 01:17 PM.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    Code:
    int d;
    if (c == '+')
        d = a + b;
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    6
    Ok, but it still says that 'd' is not declared...

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    I don't know what you did... this works:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	int a;
    
    	cout<<"Please enter a number up to a maximum of 10 billions: ";
    	cin>>a;
    
    	int b;
    
    	cout<<"Please enter another number up to a maximum of 10 billions: ";
    	cin>>b;
    
    	char c;
    
    	cout<<"What would you like to do with the numbers? ";
    	cin>>c;
    
    	int d;
    
    	if (c == '+')
    	{
    		d = a + b;
    	}
    	if (c == '-')
    	{
    		d = a - b;
    	}
    	if (c == '*')
    	{
    		d = a * b;
    	}
    	cout<<"The answer is... "<< 'd' <<"\n";
    	cin>>d;
    }
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Nov 2009
    Posts
    6
    Oh, thank you very much, my hero!
    I wonder if it would insult you if i added you as a friend if I have future problems?

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    Feel free.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Nov 2009
    Posts
    6
    And then there is one last problem. Look at this:
    Code:
    Please enter a number up to a maximum of 10 billions: 10
    Please enter another number up to a maximum of 10 billions: 10
    What would you like to do with the numbers? +
    The answer is... d
    And then the .exe ends... Maybe if i change the icons with the words?

    Sorry if I am annoying you...

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    Ah,
    cout<<"The answer is... "<< 'd' <<"\n";
    should be
    cout<<"The answer is... "<< d <<"\n";

    When referring to variables, you use their names. When referring to characters, you put them in quotes. When referring to strings, you use double quotes.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Arcantos247: the 'd' has to be outside the {}'s because a variable doesn't exist after the curly-bracket that closes the scope it is in. E.g.
    Code:
    {
        int d;
        // d exists here
        {
            int e;
            // e exists here, and so does d
        }
        // e does not exist here, but d still does
        {
            int f;
            // f exists here, and so does d
        }
        // neither e nor f exist here, but d still does
    }
    // all of the above variables no longer exist here
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  12. #12
    Registered User
    Join Date
    Nov 2009
    Posts
    6
    Thanks for the help, both of you!

  13. #13
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,438

    Structure

    Stick with it! you will soon be rewarded with satisfaction of seeing your ideas realised, and your skills will grow quickly, i would recommend declaring all the variables you plan to use at top of main for now, to avoid the confusion explained earlier

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    I recommend that you actually learn about the concept of scopes. It will help you along the way. And it is a very important concept.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 05:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 09:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21