Thread: Noob with super quick question.

  1. #1
    Registered User
    Join Date
    Mar 2019
    Posts
    50

    Noob with super quick question.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    	
    	int age1, age2, age3, average;
    	age1 = age2 = 4;
    	
    	printf("Enter your age.\n");
    	scanf("%d\n", &age1);
    	
    	average = (age1 + age2 + age3) / 3;
    	printf("Group average age: %d\n", average);
    	
    	
    	return 0;
    }
    Ok, in this program, I知 ABSOLUTELY Sure you guys know what I知 trying to do here. Every once in a while though, it seems my compiler is throwing away one of the variables in my equation. When I put 4 as input, it gives 2 as output. Seems like it痴 dropping one of the age variables. I知 using cpp compiler on my iPhone as I知 stuck without a computer for a couple months while I save up as my old pc痴 mobo went out (RIP, lol). Anyways I ALWAYS try my best to research my issues and only ask for help if I really can稚 find it anywhere else. If I did something stupid please be easy on me. I did try to make sure I wasn稚 wasting any bodies time, but I知 still pretty brand new at this. Thanks.
    Last edited by Ctylersills; 03-18-2019 at 12:23 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You don't seem to have read in the values for age2 and age3. You assigned them to age1 before giving them initial values, which is wrong.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2019
    Posts
    50
    That DAMN selective dyslexia AGAIN! Haha, lol. Thanks, totally makes since after I read your reply I caught it. Gosh I知 trying so hard to avoid doing things like this but every so often I find myself making a headslap. It so funny because I always catch the big things, but I hope I get better with the small minutiae I miss sometimes. Thanks for being patient with me!

  4. #4
    Registered User
    Join Date
    Apr 2008
    Location
    USA
    Posts
    24
    Tested your code under an assumption that initialization skipping over age3 was the issue; it was.

    age3 uninitialized resulted in average equaling 0 (where age1 & age2 were both initialized to 4); explicitly setting age3 to 0 and performing the average with age1 & age2 initialized to 4 resulted in 2 (int) correctly. You generally get compiler warnings about uninitialized variables so you can go verbose with Visual Studio as well as any number of others like Code::Blocks using clang/gcc simply setting compiler output flags like -Wall or -Weverything. Had fun with this one.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Super NOOB Question of Day
    By andyouf in forum General Discussions
    Replies: 3
    Last Post: 06-05-2014, 06:17 AM
  2. quick noob question
    By thanatos1 in forum C# Programming
    Replies: 2
    Last Post: 06-17-2009, 08:28 PM
  3. super noob question
    By verbity in forum C++ Programming
    Replies: 6
    Last Post: 06-23-2007, 11:38 AM
  4. Hello everyone, super noob!
    By TiznaraN in forum C++ Programming
    Replies: 14
    Last Post: 09-30-2006, 11:29 PM
  5. Super quick ? here
    By CAP in forum C Programming
    Replies: 9
    Last Post: 08-03-2002, 12:35 PM

Tags for this Thread