coding in C

March 5, 2009 at 05:06:27
Specs: vista
how do I get C to read a full sentence from users input ? in other words

printf("how was your day?");
user would reply: my day was great
but the program keeps collecting only the first word. "my"


See More: coding in C

Report •


#1
March 5, 2009 at 05:42:28
What is your code to read the input from the user?

Report •

#2
March 5, 2009 at 06:36:02
yes please

Report •

#3
March 5, 2009 at 06:37:04
This is what it looks like basically

#include <stdio.h>


int main ()
{
char answer;
char name[20];
int age;
char weather;
char sunny = 's';
char rainny = 'r';
char cloudy = 'c';
char windy ='w';
char emotions[30];
char colours[30];
char hobbies[20];
char gender [2];
char blue;
char colour;

printf("Welcome to Heartbeat 1.0 Trial version\n\n");

printf("Would you like to chat? Y/N ");

scanf("%c", &answer);

if ((answer == 'y') || (answer == 'Y'))
{
printf("\n So what's your name? ");
scanf("%s", &name);

printf("\n Hello %s", &name);
printf("\n \n Are you M/F? ");

scanf("%s", &gender);

if ((gender [0] == 'F' ) || (gender[0] == 'f'))
{
printf("\n My name is Neil, and it's nice to meet you I think you beautiful.");
}
else
{
printf("\n My name is Tina, and it's nice to meet you are so handsome.");
}

printf("\n \n How was your day? ");
scanf("%s", &emotions);

printf("\n How comes your day was so %s? ", &emotions);
scanf("%s",&emotions);

printf("\n How old are are you? ");

scanf( "%d", &age );

if ( age < 40 )
{
printf ("\n You are pretty young!" );
}
else
if ( age == 40 )
{
printf("\n \n you are in your golden years");
}
else
{
printf("\n \n You are really old" );
}

printf("\n \n well I am 30yrs young, lol.");

printf("\n \n I like going to the movies and meeting people.");

printf("\n \n so, am what do you like doing? ");

scanf("%s", &hobbies);

printf("\n \n Ok cool, sounds interesting.");

printf("\n \n do you have a favorite colour, mine is blue? ");

scanf("%s", &colours);

printf("\n \n okay, hope you don't mind me asking all these questions, just trying to get to know you better? Y/N ");

scanf("%s", &answer);

if ((answer == 'Y') || (answer == 'y'))
{
printf("\n sorry, we will talk later.");

printf("\n Thank you for using Heartbeat 1.0 trial.");
}
else
{
printf("\n great that means I can get to spend more time with you.");
}

printf("\n \n I heard some good actions are out have you seen Taken? y/n ");

scanf("%s", &answer);

printf("\n \n we need to do this more often, I love gazing into your eyes.");

printf("\n did i tell you much i enjoy talking to you? ");

scanf("%s", &answer);

printf("\n How is the weather outside? ");

printf("\n\n please, enter S for sunny,R for rainny,C for cloudy or W for windy so i can understand you.");

scanf("%s",&weather);

switch (weather) {

case 's':
printf("\n wow a day for the beach, do you go what's it like? ");
break;
case 'r':
printf("\n well,if you are leaving home you need to take your coat and umbrella.");
printf("\n I don't want you to get sick on me.");
break;
case 'c':
printf("\n looks like you are going to get some rain dear.");
break;
case 'w':
printf("\n in that case, stay in doors before you get blown away.");
break;
default:
printf("\n Whoa! i don't understand what that means, can you tell me wether it, rainy, cloudy, windy or sunny.");
printf("\n thanks.");
break;
}

printf("so, am are you married, seeing anyone right now Y/N? \n \n");

scanf("%c", &answer);

if ((answer == 'Y' || answer == 'y' ))

{
printf("\n \n Dam, I knew it, all the good one are taken.");
printf("\n \n but atleast we can be friends.\n\n");
}

else
{
printf("\n \n Do you have kids? Y/N\n \n");
scanf("%c", &answer);
}

if ((answer =='Y'|| answer =='y'))
{
printf("\n \n How many? \n \n");
}
else
{
printf("don't you like kids? Y/N \n \n");
}
}

else
{
printf("\n Okay Bye,Bye");

printf("\n Thank you for using Heartbeat 1.0 trial");
}

getchar();
getchar();

return 0;
}


Report •

Related Solutions

#4
March 5, 2009 at 07:00:54
I see you are using scanf() to read the input but this will read input until it sees a white space. If you use
gets() to read the input it will stop reading at a CR and include the white space in the string.
The syntax is gets(string) where string is the var to hold the text.

Report •

#5
March 5, 2009 at 10:15:22
how do I send the log on time and log off time to a printer

Report •

#6
March 5, 2009 at 10:16:26
And thank you very much for the previous answer

Report •

#7
March 5, 2009 at 10:35:24
Be extremely careful when using gets. If your string does not have enough capacity and the user enters a very long line, gets will overwrite other parts of your program and cause undefined results (such as a crash or even worse.)

Report •

#8
March 5, 2009 at 10:52:55
ok thank you

Report •

#9
March 5, 2009 at 17:16:15
You should never use gets. Use fgets instead.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[]) {
   
   char name[20] = { 0 }, *p;
   
   printf("Enter name: ");
   if(fgets(name, sizeof name, stdin) != NULL) {
      if((p = strchr(name, '\n')) != NULL) {
         *p = '\0';
      } else {
         while(getchar() != '\n'){}
      }
   } 
   
   puts(name);   
   return (0);
}

Also, should you see fflush(stdin); then don't use it as its behavior is undefined for input streams. Anything could happen.

Similarly, avoid using ints, floats for direct user input... use a char array instead and convert to the appropriate type. Your program is safer that way because you never know if a user will follow instructions.

int num = 0;
char nbuf[10] = { 0 };

printf("Enter an integer: ");
fgets(nbuf, sizeof nbuf, stdin);
if((sscanf(nbuf, "%d", &num)) != 1) {
   printf("invalid input %s", nbuf);
} else {
   printf("you entered %d\n", num);
}

sscanf is handy because it returns the number of successful conversions and not a value. For example, the atoi function will return 0 on failure, but 0 may actually be a valid result.

HTH


Report •

#10
March 6, 2009 at 03:46:19
If using fgets then the newline character will be in the input buffer (assuming the input string is fewer chars than the size of the buffer). If you use gets it is discarded.
But it's a simple matter to write some code to strip it off.

Report •


Ask Question