Thread: Whatís going on here? I think variable problems.

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

    Whatís going on here? Int variable problems.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <string.h>
    #include <math.h>
    
    int main()
    {
    	int age;
    	
    	puts("How old was Methusalah?");
    	scanf(&age);
    	printf("Okay, so...Methusalah was %d years old.", age);
    	
    	
    	return 0;
    Just 2 days ago I was writing MUCH more complex programs. Now I canít get a basic int variable to store input from the user. Wtf have I done wrong? It keeps saying Methuselah was 0 years old, no matter the input value.
    Last edited by Ctylersills; 03-28-2019 at 12:11 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,664
    > scanf(&age);
    If you're not getting warnings / errors from your compiler, change your compiler.
    Code:
    $ gcc bar.c
    bar.c: In function ‘main’:
    bar.c:12:11: warning: passing argument 1 of ‘scanf’ from incompatible pointer type [-Wincompatible-pointer-types]
         scanf(&age);
               ^
    In file included from /usr/include/features.h:367:0,
                     from /usr/include/stdio.h:27,
                     from bar.c:1:
    /usr/include/stdio.h:446:12: note: expected ‘const char * restrict’ but argument is of type ‘int *’
     extern int __REDIRECT (scanf, (const char *__restrict __format, ...),
                ^
    bar.c:12:11: warning: format not a string literal and no format arguments [-Wformat-security]
         scanf(&age);
               ^
    It's
    scanf("%d",&age);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2019
    Posts
    50
    Sorry for the stupidity, I knew that but just had a temporary brainfart. If I had codeblocks Iíd be way better off with extra warnings and such. UNFORTUNATELY... Iím stuck coding on iPhone to practice for now and the cpp compiler tends to just let things like that slip through. Itís my fault though I shouldíve opened one of a dozen programs I have with integers and checked. I was reading a book and it was talking about turning numbers into values with atoi function and I just got strings and numbers and integers blanked out in my head. I was thinking for some reason it was taking it as a string even though I used the ď=ď, which obviously means that couldnít be.. The thing is I find if I take a one day break from the practical part to just focus on theory I find I go completely lame and rusty. Sorry for the waste of time.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    3,747
    You need to learn about your compiler! And, if your compiler have no warning options you need to find a better compiler!

    "A poor tradesman always blames his tools"

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    Mar 2019
    Posts
    50
    Well, Iím sure that would be best. The problem is iOS/iPhone only has 4 compilers. The first one was GOD awfuly filled with ads. The second wanted me to pay to run every program after the first 10. The third had errors all the time that made no sense. This one works closest to perfect as I can get without the swift skills to make a new compiler app. But as I said Iím not complaining just stating a fact. Some might just give up and make the excuse, ďI donít have a computer, so I CANT learn right now.Ē At least Iím trying.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,664
    Well if your iphone is your only electronic device, then I suppose it will have to do.

    If all your compiler choices suck, then perhaps one of the online compilers will be better.
    C++ Shell
    Coliru
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. unused variable causes lots of problems
    By Isnalla in forum C Programming
    Replies: 1
    Last Post: 02-25-2012, 10:40 AM
  2. Replies: 4
    Last Post: 11-30-2005, 02:05 PM
  3. problems with passing ostream variable
    By paperbox005 in forum C++ Programming
    Replies: 2
    Last Post: 07-31-2004, 12:31 AM
  4. Variable Type Problems
    By Gnoober in forum C++ Programming
    Replies: 8
    Last Post: 10-14-2002, 04:28 PM
  5. Having problems incrementing variable names
    By Legolas in forum C++ Programming
    Replies: 4
    Last Post: 11-11-2001, 10:14 PM

Tags for this Thread