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

  • Context: C/C++ 
  • Thread starter Thread starter beatlesben
  • Start date Start date
  • Tags Tags
    C++
Click For Summary

Discussion Overview

The discussion revolves around a C++ programming issue related to the use of pointers and variable initialization. Participants explore the problem of an uninitialized variable 'g' leading to an error message, as well as seek clarification on the meaning of the 'return' statement in C++.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Homework-related

Main Points Raised

  • One participant notes that the variable 'g' is declared but not initialized before it is used in the expression 'x = 1 + g'.
  • Another participant suggests that the input for 'g' should be taken before it is used in calculations to avoid the error.
  • There is a clarification that 'return 1' typically indicates failure, while 'return 0' indicates success, with a suggestion to use 'EXIT_SUCCESS' for better practice.

Areas of Agreement / Disagreement

Participants generally agree on the need to initialize 'g' before its use, and there is a shared understanding of the implications of the 'return' statement, though no consensus is reached on the best practices for returning values.

Contextual Notes

The discussion highlights the importance of variable initialization in C++ and the conventions surrounding return values, but does not delve into deeper implications or alternative programming practices.

Who May Find This Useful

Beginner C++ programmers, educators teaching programming concepts, and individuals interested in understanding pointers and variable management in C++.

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();
count<< *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();
count<< *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!
 

Similar threads

Replies
3
Views
2K
  • · Replies 30 ·
2
Replies
30
Views
5K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 118 ·
4
Replies
118
Views
10K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 6 ·
Replies
6
Views
1K
  • · Replies 39 ·
2
Replies
39
Views
5K
  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 40 ·
2
Replies
40
Views
4K