473,326 Members | 2,113 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Code being skipped

Hi,
My program is skipping over the prompt to enter a first name on the
second pass thru. Any help would be appreciated:

#include <stdio.h>
#include <string.h>
#define first_name_len 30
#define last_name_len 50
void strip_newline( char *str, int size )
{
int i;
/* remove the null terminator */
for ( i = 0; i < size; ++i )
{
if ( str[i] == '\n' )
{
str[i] = '\0';
/* we're done, so just exit the function by returning */
return;
}
}
/* if we get all the way to here, there must not have been a
newline! */

}
int main()
{
char name[50];
char lastname[50];
char fullname[100]; /* Big enough to hold both name and lastname */

char cmd;

do{
printf( "Please enter your name: " );
fgets( name, 50, stdin );
/* see definition above */
strip_newline( name, 50 );
/* strcmp returns zero when the two strings are equal */
if ( strcmp ( name, "Bill" ) == 0 )
{
printf( "That's a cool name.\n" );
}
else
{
printf( "That's not my name.\n" );
}
// Find the length of your name
printf( "Your name is %d letters long", strlen ( name ) );
printf("\n");
printf( "Enter your last name: " );
fgets( lastname, 50, stdin );
strip_newline( lastname, 50 );
fullname[0] = '\0';
/* strcat will look for the \0 and add the second string starting
at
that location */
strcat( fullname, name ); /* Copy name into full name */
strcat( fullname, " " ); /* Separate the names by a space */
strcat( fullname, lastname ); /* Copy lastname onto the end of
fullname */
printf( "Your full name is %s\n\n",fullname );
printf("Enter c to continue, or q to quit");
cmd = getchar();
name[0] = '\0';
lastname[0] = '\0';
fullname[0] = '\0';
}while(cmd == 'c');
return 0;

Jan 17 '07 #1
6 1718
Bill wrote:
Hi,
My program is skipping over the prompt to enter a first name on the
second pass thru. Any help would be appreciated:
[...]
do{
[...]
printf("Enter c to continue, or q to quit");
cmd = getchar();
name[0] = '\0';
lastname[0] = '\0';
fullname[0] = '\0';
}while(cmd == 'c');
What do you type, key for key, when you wish to continue?

Jan 17 '07 #2

"Bill" <bi*********@allstate.comwrote in message
news:11**********************@38g2000cwa.googlegro ups.com...
Hi,
My program is skipping over the prompt to enter a first name on the
second pass thru. Any help would be appreciated:
Please see my reply at 'alt.comp.lang.learn.c-c++'

In the future, if you want to post the same message
to more than one newsgroup, don't send it separately
to each group, cross-post instead. Thank you.

-Mike
Jan 17 '07 #3
c and ENTER, which goes into the first name variable. I tried fgets,
but then the program just executes once and quits.

Harald van Dijk wrote:
Bill wrote:
Hi,
My program is skipping over the prompt to enter a first name on the
second pass thru. Any help would be appreciated:
[...]
do{
[...]
printf("Enter c to continue, or q to quit");
cmd = getchar();
name[0] = '\0';
lastname[0] = '\0';
fullname[0] = '\0';
}while(cmd == 'c');
What do you type, key for key, when you wish to continue?
Jan 17 '07 #4
Bill wrote:

Please don't top-post. I changed it here.
Harald van Dijk wrote:
Bill wrote:
Hi,
My program is skipping over the prompt to enter a first name on the
second pass thru. Any help would be appreciated:
[...]
do{
[...]
printf("Enter c to continue, or q to quit");
>
>
cmd = getchar();
name[0] = '\0';
lastname[0] = '\0';
fullname[0] = '\0';
>
>
}while(cmd == 'c');
What do you type, key for key, when you wish to continue?

c and ENTER, which goes into the first name variable. I tried fgets,
but then the program just executes once and quits.
I hoped that your answer would be enough for you to figure it out
yourself. getchar() reads a single character. You enter two characters.
The second character remains unread as you restart the loop.

Jan 17 '07 #5
Bill wrote:
c and ENTER, which goes into the first name variable. I tried fgets,
but then the program just executes once and quits.
Call getchar twice. The first time will get the 'c', the second time
will get the '\n' (ENTER).

--
Simon.
Jan 17 '07 #6

Simon Biber wrote:
Bill wrote:
c and ENTER, which goes into the first name variable. I tried fgets,
but then the program just executes once and quits.

Call getchar twice. The first time will get the 'c', the second time
will get the '\n' (ENTER).

--
Simon.
Works like a champ! Thanks!

Jan 17 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Remy Blank | last post by:
Hello unittest users, In a project I am working on, I have a series of tests that have to be run as root, and others as a normal user. One solution is to separate the tests into two different...
0
by: Remy Blank | last post by:
Ok, here we go. I added the possibility for tests using the unittest.py framework to be skipped. Basically, I added two methods to TestCase: TestCase.skip(msg): skips unconditionally...
0
by: Bon | last post by:
Hello All I got "Tables are skipped or export failed" error when I used upsizing wizard on migrating MS Access 2000 to MS SQL Server 2000. I am changing my application backend database from MS...
0
by: Xiaofeng | last post by:
I got the error message "project configuration skipped because it is not loaded" when using enterprise templates. Anybody know what happened?
8
by: nick | last post by:
i use sscanf()to get the words in a line, it will skip all the space automatically, if i want to know how many spaces were skipped and get the words in a line, what can i do? thanks!
3
by: swingingming | last post by:
Hi, I made an access application. And in some cases, it gives me error. And when I tried step by step execution, it worked fine. And every time afterwards. But it gave me the wrong answer before I...
2
by: hup | last post by:
One of my Linux (CentOS) server stop interpret php code any more. I did try to compile and installed other version of php, but it's still not working. # cat /tmp/phpinfo.php <? phpinfo() ?>...
27
by: pamela fluente | last post by:
Hello, for the following code, VS suggests that construct (width= height= ) is out of date and a newer one is recommended: <table><tr><td width="92" height="38" valign="middle">AnyThing</td></...
0
by: Maxim | last post by:
I use a PDO-MYSQL and executing a query like 'LOAD DATA INFILE ...'. If I execute this query with mysql_query() I can use mysql_info() to get info like "String format: Records: 42 Deleted: 0...
11
by: minishilpi | last post by:
I have a question - I have this code below to send an email and I have referenced the Microsoft CDO Library 2000. It doesn't throw any exception in the console window. It goes throughout the code...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.