Thread: Im learning C++

Results 1 to 15 of 15
  1. #1
    Lawrencecunt's Avatar
    Join Date
    Apr 2015
    Gender
    male
    Posts
    15
    Reputation
    10
    Thanks
    0
    My Mood
    Twisted

    Im learning C++

    Ok so I'm learning C++ and I just started learning, I made a simple code but it won't work. This is really simple but it just won't work for me
    --------------------------------------------------------------------------------------------------------------
    #include <iostream>
    using namespace std;
    int main(int argc, char** argv)
    {
    int b = 10;
    int a;
    int sum = a + b;

    cout << "give a number";
    cin >> a;
    cout << sum;

    return 0;
    }
    --------------------------------------------------------------------------------------------------------------
    It keeps giving the output as 10 no matter what "a" is

  2. #2
    sunkist0's Avatar
    Join Date
    Feb 2011
    Gender
    male
    Posts
    394
    Reputation
    37
    Thanks
    5,194
    Quote Originally Posted by Lawrencecunt View Post
    Ok so I'm learning C++ and I just started learning, I made a simple code but it won't work. This is really simple but it just won't work for me
    --------------------------------------------------------------------------------------------------------------
    #include <iostream>
    using namespace std;
    int main(int argc, char** argv)
    {
    int b = 10;
    int a;
    int sum = a + b;

    cout << "give a number";
    cin >> a;
    cout << sum;

    return 0;
    }
    --------------------------------------------------------------------------------------------------------------
    It keeps giving the output as 10 no matter what "a" is
    move
    Code:
    cout << "give a number";
    cin >> a;
    here
    Code:
    int main(int argc, char** argv) 
    {
     cout << "give a number";
    cin >> a;
    	int b = 10;
    	int a;
    	int sum = a + b;
    your math operations are before your input, so it's adding 10 to an initialized variable with no value (a = 0)
    Last edited by sunkist0; 07-12-2015 at 02:23 PM.

  3. The Following User Says Thank You to sunkist0 For This Useful Post:

    Lawrencecunt (07-12-2015)

  4. #3
    Lawrencecunt's Avatar
    Join Date
    Apr 2015
    Gender
    male
    Posts
    15
    Reputation
    10
    Thanks
    0
    My Mood
    Twisted
    Quote Originally Posted by sunkist0 View Post
    move
    Code:
    cout << "give a number";
    cin >> a;
    here
    Code:
    int main(int argc, char** argv) 
    {
     cout << "give a number";
    cin >> a;
    	int b = 10;
    	int a;
    	int sum = a + b;
    your math operations are before your input, so it's adding 10 to in initialized variable with no value (a = 0)
    It didn't work but i fixed it by moving
    int a;
    to the very top
    Thank you very much

  5. #4
    sunkist0's Avatar
    Join Date
    Feb 2011
    Gender
    male
    Posts
    394
    Reputation
    37
    Thanks
    5,194
    Quote Originally Posted by Lawrencecunt View Post
    It didn't work but i fixed it by moving
    int a;
    to the very top
    Thank you very much
    oh yeah forgot to say move a to the top, glad you got it figured out.

  6. #5
    BotilyBotBot's Avatar
    Join Date
    Jul 2015
    Gender
    male
    Posts
    4
    Reputation
    10
    Thanks
    0
    My Mood
    Confused
    what you have done is a+b before you got a value for a.
    if you want it to work you need to first get a value for a and then add it to something

  7. #6
    CoBraX's Avatar
    Join Date
    Dec 2012
    Gender
    male
    Posts
    33
    Reputation
    10
    Thanks
    6
    You are doing it all wrong, you support to get a error trying to use "a" because it's not initialize.


    Code:
    #include <Windows.h>
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	SetConsoleTitle(L"TutorialApplication");
    	
    	int b = 10;
    	int a;
    	int sum = a + b;
    
    	cout <<"Give a number: ";
    	cin >> a;
    	cout << sum;
    	cin.get();
    	return 0;
    }
    Right code below. What you support to be doing is trying to get the sum of a + b.
    Try to give "a" a value like you did "b".
    Code:
    #include <Windows.h>
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	SetConsoleTitle(L"TutorialApplication");
    	
    	int b = 10;
    	int a = 10;
    	int sum = a + b;
    
    	cout <<"Give a number: ";
    	cin >> sum;
    	cout << sum;
    	cin.get();
    	return 0;
    }

    Part 1: Introduction to C++ Programming: Done
    Part 2: Becoming a Functional C++ Programmer: Done
    Part 3: Introduction to Classes: Done

    Part 1: Window API Programming Basic: Started
    Part 2: Creating Simple Application: Not started
    Part 3: Graphic Interface Device: Not started

  8. #7
    InunoTaishou's Avatar
    Join Date
    Jan 2009
    Gender
    male
    Location
    The Internet
    Posts
    446
    Reputation
    20
    Thanks
    950
    My Mood
    Relaxed
    I don't really know why 4 people had to answer a question about the declaration of a variable.
    https://www.mpgh.net/forum/signaturepics/sigpic210976_1.gif

  9. #8
    CoBraX's Avatar
    Join Date
    Dec 2012
    Gender
    male
    Posts
    33
    Reputation
    10
    Thanks
    6
    Lol so he can see go with which one he feel like he like the best. & beside you should be happy be are willing to share some knowledge.

    Part 1: Introduction to C++ Programming: Done
    Part 2: Becoming a Functional C++ Programmer: Done
    Part 3: Introduction to Classes: Done

    Part 1: Window API Programming Basic: Started
    Part 2: Creating Simple Application: Not started
    Part 3: Graphic Interface Device: Not started

  10. #9
    殺す必要がある唯一のものは殺されるために準備され人 々である。
    Premium Member
    Hitokiri~'s Avatar
    Join Date
    Oct 2012
    Gender
    female
    Location
    Cancer.
    Posts
    1,201
    Reputation
    24
    Thanks
    937
    My Mood
    Bitchy
    Quote Originally Posted by InunoTaishou View Post
    I don't really know why 4 people had to answer a question about the declaration of a variable.
    And what makes me cry is all the answers are wrong in the first place.

    Code:
    #include <iostream>
    using namespace std; 
    int main(int argc, char** argv) 
    {
      int b = 10;
      int a;
      int sum;
    
      cout << "give a number";
      cin >> a;
    
      // Calculate sum only AFTER you get a value for a.
      sum = a + b;
      cout << sum;
    
      return 0;
    }
    It's like people don't even do pre-school math anymore. How can you calculate the sum for a variable when you don't know the value of the variable ?

  11. #10
    InunoTaishou's Avatar
    Join Date
    Jan 2009
    Gender
    male
    Location
    The Internet
    Posts
    446
    Reputation
    20
    Thanks
    950
    My Mood
    Relaxed
    Quote Originally Posted by CoBraX View Post
    Lol so he can see go with which one he feel like he like the best. & beside you should be happy be are willing to share some knowledge.
    You realize yours was wrong right?

    Code:
    	cin >> sum;
    	cout << sum;
    All you're doing is printing the user input, you're not doing the sum of a and b... It completely defeats the purpose of what he was trying to do with a, b, and sum = a + b.

    There are numerous ways this could be done. What OP needs to understand though is once you define a declare a variable and initialize it (int a is declare, sum = a + b is defining) it stays that value until you redefine it (i.e., when you tell that variable to be something else)

    When you first wrote sum = a + b; you made sum be the value of a + b at that point. It does not change until you make it change.

    Code:
    #include <iostream>
    #include <functional>	// std::plus<Type>()(variable, variable)
    
    int add(int first, int second)
    {
    	return first + second;
    }
    
    int main()
    {
    	// Declare b and sum, declare a AND initialize it to the value of 10
    	int a = 10;
    	int b, sum;
    	
    	std::cout << "Give me a number: ";
    	std::cin >> b;
    
    	// std::plus<type> guarantees the addition of pointers as well as variables
    	sum = std::plus<int>()(a, b);
    	// Declare sum up there, initialize it right here
    	sum = a + b;
    	// Declare sum up there, pass the variabls to another function to do the math for us
    	sum = add(a, b);
    
    	std::cout << sum << std::endl;
    
    	// main() does not have to return a value
    	// return 0;
    }
    - - - Updated - - -

    Quote Originally Posted by Hitokiri~ View Post

    And what makes me cry is all the answers are wrong in the first place.
    Pretty much what I was thinking after reading all the comments.
    https://www.mpgh.net/forum/signaturepics/sigpic210976_1.gif

  12. The Following User Says Thank You to InunoTaishou For This Useful Post:

    Hitokiri~ (07-13-2015)

  13. #11
    殺す必要がある唯一のものは殺されるために準備され人 々である。
    Premium Member
    Hitokiri~'s Avatar
    Join Date
    Oct 2012
    Gender
    female
    Location
    Cancer.
    Posts
    1,201
    Reputation
    24
    Thanks
    937
    My Mood
    Bitchy
    Quote Originally Posted by InunoTaishou View Post
    ...
    One thing I had to add was

    - Main doesn't need to return a value but if you declare it as int main() you need to at least return something or it'll throw an error/warning ( Cant remember which )
    - void main() doesn't require anything returned denoted by its void type.

  14. The Following User Says Thank You to Hitokiri~ For This Useful Post:

    InunoTaishou (07-13-2015)

  15. #12
    CoBraX's Avatar
    Join Date
    Dec 2012
    Gender
    male
    Posts
    33
    Reputation
    10
    Thanks
    6
    @InunoTaishou, thank you for correcting me. After all that's why we are here to learn & share.

    Part 1: Introduction to C++ Programming: Done
    Part 2: Becoming a Functional C++ Programmer: Done
    Part 3: Introduction to Classes: Done

    Part 1: Window API Programming Basic: Started
    Part 2: Creating Simple Application: Not started
    Part 3: Graphic Interface Device: Not started

  16. #13
    AuT03x3C's Avatar
    Join Date
    Nov 2014
    Gender
    male
    Location
    kernel32.dll
    Posts
    453
    Reputation
    11
    Thanks
    4,561
    My Mood
    Sleepy
    This thread made me laugh .

  17. #14

  18. #15
    aznanimality's Avatar
    Join Date
    Aug 2008
    Gender
    male
    Location
    628
    Posts
    60
    Reputation
    10
    Thanks
    1
    Quote Originally Posted by Hitokiri~ View Post


    One thing I had to add was

    - Main doesn't need to return a value but if you declare it as int main() you need to at least return something or it'll throw an error/warning ( Cant remember which )
    - void main() doesn't require anything returned denoted by its void type.
    This is correct, though the program will still run if you don't put a return value

Similar Threads

  1. I want learn!!!
    By rafaelstefany in forum Hack Requests
    Replies: 1
    Last Post: 05-04-2016, 06:21 AM
  2. Willing To Learn
    By Dewd In The Newd in forum Gate To Heaven Hacks
    Replies: 13
    Last Post: 09-27-2007, 08:40 AM
  3. Where could I learn C++? (Beginner, and Advanced stuff)
    By TsumikiriX in forum C++/C Programming
    Replies: 8
    Last Post: 07-19-2006, 08:11 PM
  4. Learn Hacking
    By Loler in forum Hack Requests
    Replies: 2
    Last Post: 01-22-2006, 03:20 PM
  5. Looking to learn.
    By SadisticGrin in forum Hack Requests
    Replies: 1
    Last Post: 01-15-2006, 06:57 PM