2
votes

I was given this exercise:

Imagine, somebody wants to write a letter. Each sentence is entered using a single scanf-command execution. Each sentence is to be stored in a character array of optimal length. If the user enters a line which only consists of a dash (-), the text is assumed to be finished. After all text has been entered, it is to be printed out to the console.

I couldn't quite get what it means but I decided to try and write an email using scanf

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

char name1[100];       
char mail1[9999];     
char name2[100];        
printf("Dear ");      
scanf("%s", name1);     
scanf("%s", mail1);      
printf("Sincerely,");         
scanf("%s", name2);       


return 0;      
}

This is what I could write. After I write someone's name after it prints "Dear", I just write the body of the letter but if I write more than one word it doesn't let me write anything after Sincerely

2
The %s format specifier reads space-delimited words. - Some programmer dude
You should use fgets. - Fiddling Bits
Please don't make more work for other people by vandalizing your posts. By posting on the Stack Exchange network, you've granted a non-revocable right, under the CC BY-SA license, for Stack Exchange to distribute that content (i.e. regardless of your future choices). By Stack Exchange policy, the non-vandalized version of the post is the one which is distributed. Thus, any vandalism will be reverted. If you want to know more about deleting a post please see: How does deleting work? - Dharman

2 Answers

3
votes

your program should do the following:

while (1)
{
    scnaf into a fixed length buffer;
    if the buffer equals to "-"
        break the loop;
    duplicate the buffer;
    add the duplicate to any structure (like a linked list);
}

run over the list and print all the lines.

regarding scanf, a naive call will not work since it reads only aword. Use fgets or a more permissive scan format specifier. Googling it I found this option:

scanf("%[^\n]", str);
3
votes

Your 3rd scanf skipped because you have put spaces between in second scanf. To avoid this use get() method instead of scanf()

#include <stdio.h>

int main()
{
    char name1[100];       
    char mail1[9999];     
    char name2[100]; 

    printf("Dear "); 

    gets(name1);
    gets(mail1);

    printf("Sincerely, "); 

    gets(name2);

    return 0;   
}

But If you want to try this using only scanf() follow this code.

#include <stdio.h>

int main()
{
    char name1[100];       
    char mail1[9999];     
    char name2[100]; 

    printf("Dear ");

    scanf("%[^\n]%*c", name1);
    scanf("%[^\n]%*c", mail1);

    printf("Sincerely, "); 

    scanf("%[^\n]%*c", name2);

    return 0;   
}

output of both codes -:

Dear Son                                                                                                            
I love you                                                                                                          
Sincerely, Your Mom