![]() |
| | #1 |
| Open to suggestions Join Date: Mar 2003
Posts: 204
| ![]() Code: #include <stdio.h>
#include <ctype.h>
int main(void)
{
static char line[1001]; /* static to initialize as empty */
char name[101];
int lastc;
int age;
while (1)
{
printf("Tell me your name and age: ");
fflush(stdout);
/* Get a line. Extra characters treated as junk */
if (scanf(" %1000[^\n]%*[^\n]", line) >= 0)
getchar();
/* sscanf leaves a trailing space in 'name' if the
name and age are separated by a space. 'lastc' marks that space */
if (sscanf(line, "%[^0-9] %n %d", name, &lastc, &age) != 2)
fprintf(stderr, "Unable to process. Example input: John Doe 37\n");
else
{
if (isspace(name[lastc - 1]))
name[lastc - 1] = '\0';
printf("Hey, %s! You're %d years old!\n", name, age);
break;
}
}
return 0;
}
__________________ p.s. What the alphabet would look like without q and r. |
| Brighteyes is offline | |
| | #2 |
| Registered User Join Date: Mar 2003
Posts: 3,903
|
gg |
| Codeplug is offline | |
| | #3 |
| End Of Line Join Date: Apr 2002
Posts: 6,240
| >>static char line[1001]; /* static to initialize as empty */ To initialise as "empty", do it this way: >>char line[1001] = {0}; [edit] Doh, beat by miles on that one Damn work getting in the way again
__________________ When all else fails, read the instructions. If you're posting code, use code tags: [code] /* insert code here */ [/code] |
| Hammer is offline | |
| | #4 |
| Just Lurking Join Date: Oct 2002
Posts: 5,006
| Note that %[0-9] may be interpreted as equivalent to %[0123456789] as a common extension, but this is not standard.
__________________ 7. It is easier to write an incorrect program than understand a correct one. 40. There are two ways to write error-free programs; only the third one works.* |
| Dave_Sinkula is offline | |
| | #5 | ||||||||
| Open to suggestions Join Date: Mar 2003
Posts: 204
| Quote:
Code: /* Get a line. Extra characters treated as junk */
if (scanf(" %1000[^\n]%*[^\n]", line) < 0)
break;
getchar();
Quote:
Quote:
Quote:
![]() Quote:
Quote:
Quote:
Quote:
![]() Here are the changes I've made based on your suggestions Code: #include <stdio.h>
int main(void)
{
char line[1001];
char name[1001];
int age;
while (1)
{
printf("Tell me your name and age: ");
fflush(stdout);
/* Get a line. Extra characters treated as junk */
if (scanf("%1000[^\n]%*[^\n]", line) < 0)
break;
getchar();
if (sscanf(line, "%[^0123456789] %d", name, &age) != 2)
{
fprintf(stderr, "Unable to process. Example input: John Doe 37\n");
}
else
{
/* 'name' may have leading and trailing spaces */
printf("Hey, %s! You're %d years old!\n", name, age);
break;
}
}
return 0;
}
| ||||||||
| Brighteyes is offline | |
| | #6 | |
| Registered User Join Date: Mar 2003
Posts: 3,903
| Quote:
Still, making a large array static just to ensure it's zero'd out isn't as good as using "={0}", I think.
I don't follow the {} rule completely myself ![]() Code is much pretty'er btw. gg | |
| Codeplug is offline | |
| | #7 |
| Registered User Join Date: Jan 2003
Posts: 648
| <deleted> Or, if your compiler really sucks, use this to REALLY make an infinite loops without any processing (other than the jump needed to loop backwards). Code: for (;;)
{
}
EDIT:: LOL, I thought I was in the C++ board so i was doing all this cout, cin, while (true) stuff. hahahah :D Last edited by Speedy5; 03-28-2003 at 03:20 PM. |
| Speedy5 is offline | |
| | #8 |
| End Of Line Join Date: Apr 2002
Posts: 6,240
| >>this is assuming you're programming in C++ since you posted in a C++ board Errr.... what are you on about??
__________________ When all else fails, read the instructions. If you're posting code, use code tags: [code] /* insert code here */ [/code] |
| Hammer is offline | |
| | #9 |
| Open to suggestions Join Date: Mar 2003
Posts: 204
| Thank you everyone for your wonderful comments, I still have one question that wasn't answered though. Code: if (sscanf(line, "%[^0123456789] %d", name, &age) != 2) Thanks a bunch! |
| Brighteyes is offline | |
| | #10 |
| End Of Line Join Date: Apr 2002
Posts: 6,240
| Personally, I'd probably write my own parser, that way you have more control over what goes on. Also, what happens if someones name is entered as two words?
__________________ When all else fails, read the instructions. If you're posting code, use code tags: [code] /* insert code here */ [/code] |
| Hammer is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Code Review for upcoming contest | Darryl | Contests Board | 2 | 02-28-2006 02:39 PM |
| Problem : Threads WILL NOT DIE!! | hanhao | C++ Programming | 2 | 04-16-2004 01:37 PM |
| True ASM vs. Fake ASM ???? | DavidP | A Brief History of Cprogramming.com | 7 | 04-02-2003 04:28 AM |
| Interface Question | smog890 | C Programming | 11 | 06-03-2002 05:06 PM |
| Who will map the scan code (inserted by VKD_Force_keys) to virtual key code? | Unregistered | Windows Programming | 0 | 02-21-2002 06:05 PM |