#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    2

    If statement and Pointer Variable errors in Test Program


    Hello everyone,
    I have made a program to test what I know so far to test if I'm getting the idea of pointers, for loops, calling functions down,

    The two errors I'm getting in the program I made is that the pointer variable gives me a weird hexadecimal looking value: 0012FF60

    and the fact that it will go ahead and take in a number less than 1 or greater than 9 even though I made an if statement to prevent the user from even doing so.

    Otherwise, it totally works and looks good, but the conditions I made are not being followed sadly enough haha.

    Here's my code:
    Code:
    #include <iostream>
    #include <Windows.h>
    #include <string>
    
    using namespace std;
    
    void printmyanswer(int a);
    
    // Goal: Basically I want the user to input a number from 1 to 9
    // and the value they enter will go to the printmyanswer function
    // that will multiply the value by 5 each time, for a total of
    // 10 times
    
    // After this function is over, I want the pointer variable
    // to reflect this new value of the input variable and also be printed
    
    // Another note, I'm trying to set it up that the integer variable,
    // named a in the printmyanswer function to be the same as the
    // input variable
    
    
    int main()
    {
    	int input = 5;
    	int *pointer;
    	
    	pointer = &input;
    
    	cout << "Please enter any number from 1 to 9: " << endl;
    	cin >> input;
    
    	if((input<=0)&&(input>=10))
    	{ cout << "Invalid number, please enter the number again: " << endl;
    	  Sleep(3000);
    	  system("CLS");
    	  return main();
    	}
    
    	pointer = &input;
    
    	printmyanswer(input);
    
    	cout << "This is the end result to the pointer variable: " << pointer << endl;
    
    	system("PAUSE");
    	return 0;
    
    }
    
    void printmyanswer(int a)
    {
    	for (int i=0; i<=9; i++)
    	{
    		a = a * 5;
    
    		cout << "Here's your altered number: " << a << " the " << i << "th time" << endl;
    
    	}
    
    }
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,162
    Rep Power
    2222
    Originally Posted by HauntJemimah
    The two errors I'm getting in the program I made is that the pointer variable gives me a weird hexadecimal looking value: 0012FF60
    I hardly ever use iostreams (never had much use for it), so I can't speak from experience, but apparently when you cout a pointer it outputs the memory address that the pointer contains; the << operator is overloaded to know how to format whatever datatype you feed it.

    Compare that to what happens when you cout a dereferenced pointer; eg, cout << *pointer;

    Originally Posted by HauntJemimah
    and the fact that it will go ahead and take in a number less than 1 or greater than 9 even though I made an if statement to prevent the user from even doing so.
    There are actually two problems there: 1) the logical problem that kept the test from ever being true, and 2) the booby trap you set for yourself that would have blown up in your face, again!

    Why are you still calling main? After it had caused you so much trouble and we explained it to you two days ago? (see your other thread, Of Stream program errors
    ). To refresh your memory:
    Originally Posted by dwise1_aol
    Never call main(). main() is the entry point for the program. The startup code calls main(), but you never do.
    Originally Posted by scorpions4ever
    Also, never recursively call main().
    Which you acknowledged:
    Originally Posted by HauntJemimah
    Okay, thank you so much guys.
    Instead, place the prompt, receiving the user's input, and testing it inside of a do-while loop. And never ever call main. Ever!

    The logic problem is the test itself: if((input<=0)&&(input>=10))
    Say it out loud: "if the input is both less than or equal to zero and greater than or equal to ten." Now please give a number, any number that is both less than 1 and greater than 9. Just one, that's all I'm asking for. No such number exists, does it? Your test will never be true, but rather always be false. That's part of the thought process you need to go through to test the logic of your conditionals, so I'm not trying to give you hard time but rather show you how to start thinking like a programmer.

    Here's how I would tend to approach the problem. Valid numbers are from 1 to 9 inclusive, so they need to be
    ( input >= 1 && input <= 9)
    to be valid; "input greater than or equal to 1 and less than or equal to 9". So invalid inputs would be the opposite of that. The opposite of >= is <, the opposite of <= is >, and the opposite of && is ||. So the opposite of our expression for valid values would be:
    ( input < 1 || input > 9)
    ; "if input is less than one or greater than 9". That was what you were trying to write, only you ANDed them instead of ORing them.

    BTW, back in the 19th century (1847) English mathematician George Boole devised a form of symbolic logic involving variables that could be true or false and operations on those variables called AND, OR, and NOT (or inversion, negation). That system is called Boolean Algrebra, which was used to design the first automated telephone switching stations and later to design the digital circuits of digital computers. It's still taught to and used by engineers and technicians and, while not required for programmers, can be useful for us too -- actually, my initial training was as a technician and while working on my computer science degree I also took some electrical engineering courses for fun; I learned Boolean algrebra and how to describe digital circuits with it in my technician training and I learned to manipulate and simplify Boolean expressions in engineering. The rules for negating an expression, which is what we did above, are found in de Morgan's Theorem. You don't need to learn Boolean algebra, but you could use it when it would come in handy.

    Now, your do-while loop. Here is what I would envision:
    Code:
    int main()
    {
    	int input = 5;
    	int *pointer;
    	
    	pointer = &input;
    
    	do
    	{
    		cout << "Please enter any number from 1 to 9: " << endl;
    		cin >> input;
    
    		if((input<=0) || (input>=10))
    		{ 
    			cout << "Invalid number, please enter the number again: " << endl;
    			Sleep(3000);
    	  		system("CLS");
    		}
    	} while ((input<=0) || (input>=10));
    There are a number of variations that you could use, but the basic idea is here.

    And never ever even dare to start to think about calling main!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    2
    Ah what a stupid mistake.

    From now on, I won't call main, sorry.

    I'll try those ideas and see if I get any errors then, thanks!

IMN logo majestic logo threadwatch logo seochat tools logo