Results 1 to 9 of 9

Thread: confused...integer compared to pointer?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2002
    Posts
    20

    confused...integer compared to pointer?

    Hi,

    I thought i'd have a play around in C++. I'm a complete newbie (in C++ not programming). I'm trying to make a simple calculator to get a grasp of the syntax. (i'm used to python and VB).

    What trying to do is declare the functions with prototype functions up the top. Get from the user if they want to do addition,subtracton,div or multi and then do it. This is where my first problem comes in. I don't understand how to do.. elifs.. or elseifs.. if c++ even does it?. And also I don't know how to compare properly using the logical operator || .. It keeps giving me the error ANSI C++ forbids comparison between pointer and integer. I'll post the gcc output and my attempt to do the program.

    Thanks very much for any help!

    Code:
    #include <iostream>
    // Simple add, subtract, mult,divide calculator
    
    int funcAdd(int,int);
    int funcSub(int,int);
    float funcDiv(int,int);
    long funcMulti(int,int);
    
    int main()
    {
        char decide;
        int first;
        int second;
        int result;
        
        std::cout << "What would you like to do: (A)dd, (S)ubtract, (D)ivide or (M)ultiply ?\n";
        std::cin >> decide;
        
        if ( (decide == "A") || (decide == "a") || (decide == "S") || (decide = "s") || (decide == "D") || (decide == "d") || (decide == "M") || (decide == "m") )
        {
    	if ((decide == "A") || (decide == "a"))
    	    std::cout << "\nAddition\n";
    	    std::cout << "\nEnter the first number:";
    	    std::cin >> first;
    	    std::cout << "\nEnter the second number:";
    	    std::cin >> second;
    	    result = funcAdd(first,second);
    	    std::cout << "\nAnswer for " << first << " + " << second << " = " << result << std::endl;
    	else
    	if ( (decide == "S") || (decide = "s") )
    	    std::cout << "\nSubtraction\n";
    	    std::cout << "\nEnter the first number:";
    	    std::cin >> first;
    	    std::cout << "\nEnter the second number:";
    	    std::cin >> second;
    	    result = funcSub(first,second);
    	    std::cout << "\nAnswer for " << first << " - " << second << " = " << result << std::endl;
    	else
    	if ( (decide == "D") || (decide == "d")	)
    	    std::cout << "\nDivision\n";
    	    std::cout << "\nEnter the first number:";
    	    std::cin >> first;
    	    std::cout << "\nEnter the second number:";
    	    std::cin >> second;
    	    result = funcDiv(first,second);
    	    std::cout << "\nAnswer for " << first << " / " << second << " = " << result << std::endl;
    	else
    	if ( (decide == "M") || (decide == "m")	)
    	    std::cout << "\nMultiplication\n";
    	    std::cout << "\nEnter the first number:";
    	    std::cin >> first;
    	    std::cout << "\nEnter the second number:";
    	    std::cin >> second;
    	    result = funcMulti(first,second);
    	    std::cout << "\nAnswer for " << first << " * " << second << " = " << result << std::endl;
        }
        else
    	std::cout "\n You did not enter a valid option";
    }
    
    int funcAdd(int x,int y)
    {
        return x + y;
    }
    
    int funcSub(int x, int y)
    {
        return x - y;
    }
    
    float funcDiv(int x, int y)
    {
        return x / y;
    }
    
    long funcMulti(int x, int y)
    {
        return x * y;
    }

    Code:
    andrew@me:~$ g++ calc.cpp -o calc 
    calc.cpp: In function `int main()':
    calc.cpp:19: ANSI C++ forbids comparison between pointer and integer
    calc.cpp:19: ANSI C++ forbids comparison between pointer and integer
    calc.cpp:19: ANSI C++ forbids comparison between pointer and integer
    calc.cpp:19: assignment to `char' from `const char *' lacks a cast
    calc.cpp:19: ANSI C++ forbids comparison between pointer and integer
    calc.cpp:19: ANSI C++ forbids comparison between pointer and integer
    calc.cpp:19: ANSI C++ forbids comparison between pointer and integer
    calc.cpp:19: ANSI C++ forbids comparison between pointer and integer
    calc.cpp:21: ANSI C++ forbids comparison between pointer and integer
    calc.cpp:21: ANSI C++ forbids comparison between pointer and integer
    calc.cpp:29: parse error before `else'
    calc.cpp:38: parse error before `else'
    calc.cpp:45: warning: assignment to `int' from `float'
    calc.cpp:47: parse error before `else'
    calc.cpp:58: parse error before string constant
    calc.cpp:59: confused by earlier errors, bailing out

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    First off, you can avoid having to type std:: all the time by adding
    using namespace std;
    directly after the includes.

    There is no elseif or elif in C++. else if works the same.

    The "can't compare" errors stem from the fact that in C++ "h" is a c-style string (a pointer to a character) while 'h' is the actual character h.

    Also your program would be better structured with a switch statement:
    Code:
    std::cin >> decide;
    switch(decide)
    {
    case 'A':
    case 'a':
      // ...
      break;
    case 'S':
    case 's':
      // ...
      break;
    // ...
    default:
      cout << "\n You did not enter a valid option";
      break;
    }
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Dec 2002
    Posts
    20
    thanks for the help!

    .. i see so lose the "" when dealing with strings in c++ then?

    so.. the switch thing works like this..?

    Code:
    switch(variable)
    {
    case: //condition one
    //statements
    break;
    case: //condition x
    //statements
    break;
    //more case and statements
    default:
    //returns anything that is not true in the switch
    }
    Again, thanks

  4. #4
    Hyperactive Member Comreak's Avatar
    Join Date
    Feb 2001
    Location
    Dis
    Posts
    319
    Yep, that's how it works. Personally, I like it better than vbs select case. What do you think nzer?

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Dec 2002
    Posts
    20
    This, it works in linux :-)

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Should work in windows too, I see no platform specifics in the code.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Dec 2002
    Posts
    20
    sorry .. lol.. i read that as "i like vbs select case better" .. so i thought you were saying you preferred VB.. so.. i agree with you then.. switch is much nicer :-)

    merry xmas .. hey i'm one of the first to see the sun on xmas how amazing.. sort of

  8. #8
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    and instead of having || for A and a but it to lower case
    \m/\m/

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Dec 2002
    Posts
    20
    you mean A && a .. doesn't that require both sides of the statement to be true though? i.e it must = 'A' and 'a'

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width