Im learning C++

Posts 1–15 of 15 · Page 1 of 1
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
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)
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
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.
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
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;
}
I don't really know why 4 people had to answer a question about the declaration of a variable.
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 ?
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.
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.
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
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.
@InunoTaishou, thank you for correcting me. After all that's why we are here to learn & share.
This thread made me laugh .
Impossible how you laugh in this thread XD
Posts 1–15 of 15 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us