Thread: simple programme trouble printf and scanf

  1. #1
    Registered User
    Join Date
    Dec 2013
    Posts
    3

    simple programme trouble printf and scanf

    Hi,

    I'm trying to get this programme to work but I can't get it to
    output the string data at the end of the line.I have copied and pasted the line in question below but it may be a prob with the prog further down.

    It reads character input ok but doesn't put one string into another
    or recognize when a string is quoted in a printf.


    Code:
    printf("%s what is your second name?  \n", surname, name2, name);
    
    
    
    #include <stdio.h>
    int main ()
    {
    
    
    char name[20];
    char name2[40];
    char surname[120];
    
    
    printf("What is your first name?");
    scanf("%s", &name);
    fflush(stdin);
    strcpy(name2, name);
    printf("%s what is your second name?  \n");
    scanf("%s", &name2);
    strcat(surname, name2);
    strcpy(surname, name2);
    fflush(stdin);
    printf("%s what is your second name?  \n", surname, name2, name);
    
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you only have one %s in the format string, you should have only one string as an argument at the end.

    There should be no & in your scanf calls (name already is a pointer all by itself -- same with name2).

    I have no idea why you are strcpy'ing from name to name2 when you then read into name2 directly after it. Similarly, trying to do a strcat before a strcpy? (And for that matter, the first strcat can't possibly work, as you can't concatenate onto a string that doesn't exist, and at that point in the program there is no string yet stored in surname.)

    fflush(stdin) isn't what you want. I don't know exactly what you do want, but that isn't it.

    I have no idea what you mean by "put one string into another". IF by recognize when a string is quoted you mean you want to be able to recognize a string with spaces in it, then %s is not for you (the whole point of a format string is that the input should match the format, and that's not the format that goes with %s).

  3. #3
    Registered User
    Join Date
    Dec 2013
    Posts
    3
    Hi tabstop,
    Thanks for your reply.
    This is what I want to do:

    Code:
    #include <stdio.h>
    int main ()
    {
    
    
    
    
    char name[20];
    char name2[40];
    char surname[120]; //dfgdfghfd
    
    
    
    
    printf("What is your first name?");       // I want the user to enter there first name after this command,
    scanf("%s", name); 		           //then read for the string and put it into to name
    fflush(stdin);			            //i was told to flush characters. why is this wrong?
    strcpy(name2, name);		 	    //i want to put name into name2 so that so that the last name and first are one entity.
    printf("%s what is your second name? \n");  // I want the user to enter there second name after this command,
    scanf("%s", &name2);                       //scan for string and put it into name
    strcat(surname, name2);			  // make surname and name2 concatenated.
    strcpy(surname, name2);                  //This command and the one above should be swapped round.
    					 //the idea was to put name2 into surname first
    fflush(stdin);				//
    printf("%s  is your full name?  \n", surname, name2, name);// i wanted to test if surname and name 
    								// and name2 had any data in them now.
    Thanks for your help so far. I am new to this as you can see and lost my
    way in one of mark virtues tutorials...

    From Dave

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    fflush(stdin) doesn't do anything (in standard C). Some systems may use it for their own purposes, but many don't. So there are two things here (1) do you actually need to flush characters? (I don't see a reason at this point why you should) and (2) if you do, read the FAQ on this very site about how to actually do so.

    If you want the last name and first name to be one entity, it was wrong to make them separate variables in the first place. If you want to read them separately and then combine them for some reason, then you can make another variable for it like you have. (I would give it a real name, though, like "fullname".) (Note that I am hard pressed to think of a reason why you would want them together; if you want to address someone by their full name, you can just print both names and be done with it.)

    Your printf on line 19 has a %s in the format, so you must give it a string (one string) to use to replace the %s with.

    Your scanf on line 20 has an extraneous &. Notice that once you get this working you make line 18 completely irrelevant, since you overwrite the name you had copied there.

    Your strcat on line 21 is horrendously broken; you can't concatenate onto surname because surname doesn't have anything in it yet!

    Your line 22 makes line 21 completely irrelevant, because strcpy will get rid of any contents of surname that it may have previously held. Notice at this point that surname will only hold the surname (the value of name2), and notice that name2 only holds the surname because line 20 erased the previous contents.

    Line 25 has one %s, so you must give it only one string. You cannot give it three strings and hope it will do something with those strings.

  5. #5
    Registered User
    Join Date
    Dec 2013
    Posts
    3
    the program has to ask the first name, then the last name and then tell the user their full name.

    [code]

    printf("What is your first name?"); // I want the user to enter there first name after this command,
    scanf("%s", name); //what does this command do?

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    Do you have to put the entire name into a single array before printing it? Or can you just print each name out individually?

    It would be a lot more helpful to be clear on the specs of your assignment.

    I'd suggest your next post consist of these two things:

    - An explanation of exactly what you need to accomplish (the expected process, the requirements, and any limitations)

    - A program that simply prompts for two names, and prints each of them out individually. If your assignment requires more than that, use this as a starting point and we can work from there.

  7. #7
    Registered User
    Join Date
    Nov 2013
    Posts
    34
    Try starting off with just the basics of the program like:

    Code:
     #include <stdio.h>
    
    int main ()
    {
        char first_name[20];
        char second_name[20];
    
        printf("\n\t What is your first name? ");
        scanf(" %s", &frist_name);
      
        printf("\n\t What is your second name? ");
        scanf(" %s", &second_name);
    
        printf("\n\t %s %s is your full name? ", first_name, second_name);
    
        exit(0);
    }
    Last edited by jkgoose937; 12-03-2013 at 01:53 PM.

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    ^ That's the work I was trying to get the OP to do themselves...

    Also, you have a typo on line 9.

  9. #9
    Registered User
    Join Date
    Nov 2013
    Posts
    34
    i didn't mean to do your work for you, but think about what's going on there. your grabbing [stdin] and placing into '&[memory]' the [memory] then gets placed it into "%s".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-02-2011, 09:17 PM
  2. simple programme to find the largest of three
    By jackson6612 in forum C++ Programming
    Replies: 19
    Last Post: 04-06-2011, 03:47 AM
  3. Simple function programme
    By peckitt99 in forum C++ Programming
    Replies: 12
    Last Post: 10-24-2006, 03:18 PM
  4. Simple Scanf and printf
    By xamlit in forum C Programming
    Replies: 9
    Last Post: 08-31-2005, 05:20 PM
  5. Simple C++ GUI programme does'nt run from dos
    By Shadowhunt in forum C++ Programming
    Replies: 4
    Last Post: 05-31-2003, 05:30 AM