Thread: Questions from a noob

  1. #1
    Registered User
    Join Date
    Mar 2018
    Posts
    1

    Questions from a noob

    My program is attached. It's not done yet, but its goal is to take a user's name, store it in an array. Take the name and store it as a variable. If the age is under 67 it'll calculate how many years the user has until they can retire. If it's over 67, it'll say they should've retired by then.



    And here's the output.

    Welcome to nameage
    Input name: abc
    Input age: Name: a╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠tíΓΣ░OPress any key to continue . . .




    I have two problems right now.
    1. Once you put in the name, it skips the input age scanf and goes immediately to printing out the name (which is printing some crazy stuff). The only way this can be avoided is by typing in only one character. I'm still new to this but I have read up on it and apparently it has something to do with converting char values into a string. Which leads me to question 2...

    2. Converting the Char array into a string completely messes up the output. On this, I think it might be a formatting problem using %s, but I'm not sure.

    This is probably such an easy answer some of you are cringing but any advice is welcome




    Edit: It'll probably easier for people to read the code since it's so small so here it is instead of having to download it

    #include <stdio.h>
    void main() {


    printf("Welcome to nameage\n");


    char name[10];
    int age;


    printf("Input name: ");
    scanf_s("%c", &name); // Why does it skip the age?


    printf("Input age: ");
    scanf_s("%d", &age);


    printf("Name: %s", name); //Why does it print weird characters?






    system("pause");
    return 0;
    }
    Attached Files Attached Files
    Last edited by DFT95; 03-08-2018 at 02:35 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,234
    It is good that you also posted your code within your post to make it more accessible than as an attachment, but you should post it within [code][/code] bbcode tags:
    Code:
    #include <stdio.h>
    void main() {
    
    	printf("Welcome to nameage\n");
    
    	char name[10];
    	int age;
    
    	printf("Input name: ");
    	scanf_s("%c", &name); // Why does it skip the age?
    
    	printf("Input age: ");
    	scanf_s("%d", &age);
    
    	printf("Name: %s", name); //Why does it print weird characters?
    
    
    
    	system("pause");
    	return 0;
    }
    The main problem is that %c is the format specification to read a character with the scanf family of functions, whereas you wish to read a string. Therefore, you should have written:
    Code:
    scanf("%9s", name);
    Notice that I used "%9s" instead of just "%s" because the field width should be specified so as to prevent buffer overflow. Furthermore, because in this context an array would be converted to a pointer to its first element, I passed name rather than &name as an argument. If you absolutely insist on using scanf_s rather than scanf, then you must make use of the "security enhancement" for reading strings and hence pass the buffer size too:
    Code:
    scanf_s("%9s", name, (unsigned)_countof(name));
    (Example derived from the online MSDN entry for scanf_s, _scanf_s_l, wscanf_s, _wscanf_s_l, so if it doesn't work, not my problem as I would have just used scanf.)

    By the way, the main function returns int, not void, and even where your compiler might permit it to have a void return type, you are explicitly returning 0 so it should be int return type anyway.

    Also, you should consider checking the return value of scanf (or scanf_s) to see if the input was successfully read.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Noob Questions.
    By beat in forum C++ Programming
    Replies: 2
    Last Post: 08-04-2011, 12:31 AM
  2. noob here with questions
    By rllove37 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2010, 03:32 PM
  3. Few more noob questions
    By SlpCtrl in forum C Programming
    Replies: 12
    Last Post: 10-14-2008, 04:32 PM
  4. 2 very noob questions
    By chasingxsuns in forum C Programming
    Replies: 20
    Last Post: 05-07-2006, 11:04 PM
  5. Noob Questions...
    By Firemagic in forum C++ Programming
    Replies: 4
    Last Post: 04-19-2006, 03:57 PM

Tags for this Thread