Need help in C++ (Basic stuff) menu

User Tag List

Results 1 to 13 of 13
  1. #1
    Moskis's Avatar Member
    Reputation
    13
    Join Date
    Nov 2008
    Posts
    38
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need help in C++ (Basic stuff)

    Hey I am coding a simple calculator at the moment in C++. I would love some help with.
    I am coding in console and I wrote this to start with:
    cout << "Would you like to calculate PLUS or MINUS? \n";
    Now I want to do so if they would write plus it would do + and else -

    I am a total noob since I started today and this is how my code looks like (it does not work)

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int a;
    	int b;
    	int sum;
    	int sum2;
    	
    	cout << "Would you like to calculate PLUS or MINUS? \n";
    * * *   char input = cin.get();
    
    	if(input == 'plus')
    	{
    	cout << "Enter a number! \n";
    	cin >> a;
    
    	cout << "Enter another number! \n";
    	cin >> b;
    
    	sum = a + b;
    	cout << "The sum of those numbers is " << sum << endl;
    	}
    	else
    {
    	cout << "Enter a number! \n";
    	cin >> a;
    
    	cout << "Enter another number! \n";
    	cin >> b;
    
    	sum2 = a - b;
    }
    	return 0;
    }
    Last edited by Moskis; 06-25-2012 at 05:35 PM.

    Need help in C++ (Basic stuff)
  2. #2
    l0l1dk's Avatar Elite User

    Reputation
    499
    Join Date
    Sep 2010
    Posts
    342
    Thanks G/R
    1/6
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by Moskis View Post
    Hey I am coding a simple calculator at the moment in C++. I would love some help with.
    I am coding in console and I wrote this to start with:
    cout << "Would you like to calculate PLUS or MINUS? \n";
    Now I want to do so if they would write plus it would do + and else -

    I am a total noob since I started today and this is how my code looks like (it does not work)

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int a;
    	int b;
    	int sum;
    	int sum2;
    	
    	cout << "Would you like to calculate PLUS or MINUS? \n";
    * * *   char input = cin.get();
    
    	if(input == 'plus')
    	{
    	cout << "Enter a number! \n";
    	cin >> a;
    
    	cout << "Enter another number! \n";
    	cin >> b;
    
    	sum = a + b;
    	cout << "The sum of those numbers is " << sum << endl;
    	}
    	else
    {
    	cout << "Enter a number! \n";
    	cin >> a;
    
    	cout << "Enter another number! \n";
    	cin >> b;
    
    	sum2 = a - b;
    }
    	return 0;
    }
    Try if(strcmp(input, "plus") == 0) instead of if(input == 'plus'). C strings (char*s) can't be reliably compared using ==.

    Also, you forgot to print out the result of the subtraction.

  3. #3
    Moskis's Avatar Member
    Reputation
    13
    Join Date
    Nov 2008
    Posts
    38
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yea I know I added that :P However I found out that if I change if(input =='plus') to if(input == 'hello') or whatever (anything but plus) it works. Something makes it screw up if I type minus or plus into the cmd... Why's that? :S

    EDIT: Also with if(strcmp(input, "plus") == 0) I get
    'input' : undeclared identifier
    Last edited by Moskis; 06-25-2012 at 06:02 PM.

  4. #4
    l0l1dk's Avatar Elite User

    Reputation
    499
    Join Date
    Sep 2010
    Posts
    342
    Thanks G/R
    1/6
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by Moskis View Post
    Yea I know I added that :P However I found out that if I change if(input =='plus') to if(input == 'hello') or whatever (anything but plus) it works. Something makes it screw up if I type minus or plus into the cmd... Why's that? :S
    Change char input = cin.get(); to char input[25];
    ZeroMemory(input, 25);
    cin >> input;


    And use " instead of '.

  5. #5
    Moskis's Avatar Member
    Reputation
    13
    Join Date
    Nov 2008
    Posts
    38
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by l0l1dk View Post
    Change char input = cin.get(); to char input[25];
    ZeroMemory(input, 25);
    cin >> input;


    And use " instead of '.
    Oh. Now ZeroMemory is undefined... :/

    edit: got it working without the "ZeroMemory(input, 25);" part
    and why the number 25? ASCII I suppose?
    Last edited by Moskis; 06-25-2012 at 06:11 PM.

  6. #6
    l0l1dk's Avatar Elite User

    Reputation
    499
    Join Date
    Sep 2010
    Posts
    342
    Thanks G/R
    1/6
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by Moskis View Post
    Oh. Now ZeroMemory is undefined... :/
    Add #include to the start of the file.

    EDIT: The 25 means that the string has a maximum length of 24 characters (+ the null character).
    Last edited by l0l1dk; 06-25-2012 at 06:14 PM.

  7. #7
    Moskis's Avatar Member
    Reputation
    13
    Join Date
    Nov 2008
    Posts
    38
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by l0l1dk View Post
    Add #include <Windows.h> to the start of the file.
    What does that even do? It seems to work without it.

    Also +4 for you!

  8. #8
    l0l1dk's Avatar Elite User

    Reputation
    499
    Join Date
    Sep 2010
    Posts
    342
    Thanks G/R
    1/6
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by Moskis View Post
    What does that even do? It seems to work without it.

    Also +4 for you!
    Windows.h holds the function declarations for the Windows API. ZeroMemory is a macro used by the Windows API, and it's in Windows.h.

  9. #9
    Moskis's Avatar Member
    Reputation
    13
    Join Date
    Nov 2008
    Posts
    38
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by l0l1dk View Post
    Windows.h holds the function declarations for the Windows API. ZeroMemory is a macro used by the Windows API, and it's in Windows.h.
    I meant the ZeroMemory thing. The calculator works now without it.

  10. #10
    l0l1dk's Avatar Elite User

    Reputation
    499
    Join Date
    Sep 2010
    Posts
    342
    Thanks G/R
    1/6
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by Moskis View Post
    I meant the ZeroMemory thing. The calculator works now without it.
    The ZeroMemory would've set the string to have no value. Sometimes variables will have random values when they're allocated unless they're initialized with a value. This can sometimes cause problems, so the ZeroMemory sets the string to have no value instead of a possibly random one.

  11. #11
    Moskis's Avatar Member
    Reputation
    13
    Join Date
    Nov 2008
    Posts
    38
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by l0l1dk View Post
    The ZeroMemory would've set the string to have no value. Sometimes variables will have random values when they're allocated unless they're initialized with a value. This can sometimes cause problems, so the ZeroMemory sets the string to have no value instead of a possibly random one.
    Thank you very much good sir!

  12. #12
    DarkLinux's Avatar Former Staff
    CoreCoins Purchaser Authenticator enabled
    Reputation
    1627
    Join Date
    May 2010
    Posts
    1,846
    Thanks G/R
    193/539
    Trade Feedback
    16 (100%)
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Just use GetAsyncKeyState(VK_xxx)

  13. #13
    mahdiciaco's Avatar Private
    Reputation
    1
    Join Date
    Jul 2012
    Posts
    9
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

Similar Threads

  1. [ArcEmu] Needing Help with some ArcEmu stuff
    By Ultimadwarf in forum World of Warcraft Emulator Servers
    Replies: 18
    Last Post: 11-07-2013, 12:53 PM
  2. I really need help back to basics????
    By Wickedest in forum WoW EMU Questions & Requests
    Replies: 5
    Last Post: 10-15-2009, 06:29 PM
  3. camera & TLS stuff 2.4.2 - need help
    By mrbrdo in forum WoW Memory Editing
    Replies: 11
    Last Post: 07-04-2008, 10:33 AM
  4. Need help with some stuff
    By jordy in forum World of Warcraft Emulator Servers
    Replies: 15
    Last Post: 06-14-2008, 04:17 AM
  5. [HELP!!!] Need help setting up and stuff...!!
    By Jayson22 in forum World of Warcraft Emulator Servers
    Replies: 4
    Last Post: 05-17-2008, 11:06 AM
All times are GMT -5. The time now is 06:29 PM. Powered by vBulletin® Version 4.2.3
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Google Authenticator verification provided by Two-Factor Authentication (Free) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search