C/C++ C++ Pointers: Solving "g is not defined" & "return" Meaning

  • Thread starter Thread starter beatlesben
  • Start date Start date
  • Tags Tags
    C++
AI Thread Summary
The discussion revolves around a beginner's C++ code that encounters an issue with variable initialization. The user is trying to use the variable 'g' before it has been assigned a value through user input. The solution provided suggests moving the line where 'g' is read from input (cin >> g;) before the line where 'x' is calculated (x = 1 + g;). This ensures that 'g' has a defined value when it is used in the calculation. Additionally, there is an explanation regarding the 'return' statement in C++, clarifying that returning 1 typically indicates failure, while returning 0 signifies success. A recommendation is made to use EXIT_SUCCESS for better clarity. The user confirms that implementing these changes resolved the issue.
beatlesben
Messages
4
Reaction score
0
Hello, I'm teaching myself C++. I'm pretty much at level one. Anyway I was trying to make a code, playing with pointers:

#include <iostream>

using namespace std;

int main()
{
int x;
int g;
int *p;
x = 1 + g;
cin>> g;
p = &x;
cin.ignore();
cout<< *p <<"\n";
cin.get();
return 1;
}


When I run the code it says that g is not defined, but g is what you have to enter, so I don't understand how to define it.

Also could someone explain "return." I don't understand what the value next to it means.

Thanks,
Remember, this is my second day of learning this...
 
Technology news on Phys.org
beatlesben said:
Hello, I'm teaching myself C++. I'm pretty much at level one. Anyway I was trying to make a code, playing with pointers:

#include <iostream>

using namespace std;

int main()
{
int x;
int g;
int *p;
x = 1 + g;
cin>> g;
p = &x;
cin.ignore();
cout<< *p <<"\n";
cin.get();
return 1;
}


When I run the code it says that g is not defined, but g is what you have to enter, so I don't understand how to define it.

Also could someone explain "return." I don't understand what the value next to it means.

Thanks,
Remember, this is my second day of learning this...

Hi Beatlesben,

You've properly declared the variable g as integer, so that's set aside a piece of memory for the variable g. However, you have not specified the value of g until:

cin>> g;

When you get to the line of code that says:

x = 1 + g;

C++ does not have a value for g at this time. To fix this, make sure to put the

cin>> g;

before the:

x = 1 + g;

This way, you'll properly read in the value and place it into the variable g, and then you can use g for any future calculations.


----

As a side note, you're already teaching yourself pointers and address references? You must be a fast learner.
 
You're assigning x to 1 + g before g has been read.

Also return 1 typically means failure while 0 is success. A better approach is to #include <cstdlib> and return EXIT_SUCCESS
 
Last edited:
Thanks for the quick responses.
It worked perfectly!
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Back
Top