Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 10th, 2005, 7:30 PM   #1
Havoc_101
Newbie
 
Join Date: Apr 2005
Posts: 19
Rep Power: 0 Havoc_101 is on a distinguished road
Post Problem with strings...

Im very new to the whole C/C++ Things, but I am very good at Java so things arent that hard..
Um when I ask the user to put in their adress, and then I want it to print out the adress they entered it will only print everything they type in till a space....

char name[20], color[19], adress[20];
int x;
using namespace std;

int main(int argc, char *argv[])
{
       int n[5];
       int *prt;
       prt = &n[0];
       *prt = 3;
       printf("What is your favorite number?");
       scanf("%d", &x);
       if(x>5)
                {
                printf("The number you chose is greater than 5!\n");
                }
       printf("\nWhat is your favorite color?");
       scanf("%s", &color);
       printf("\nWhats your Adress?");
       scanf("%s", &adress);
       printf("%s\n", adress);
       printf("%d\n", *prt);
    system("PAUSE");
    return EXIT_SUCCESS;
}
Havoc_101 is offline   Reply With Quote
Old Apr 10th, 2005, 7:47 PM   #2
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
You could simply use the gets() method, instead of using scanf(). I never liked using scanf() for strings.
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Apr 10th, 2005, 8:36 PM   #3
Havoc_101
Newbie
 
Join Date: Apr 2005
Posts: 19
Rep Power: 0 Havoc_101 is on a distinguished road
The gets() doesnt seem to work for me? It gives me an error on the stdio.h ? I am just replacing the scanf("%s", adress); to gets("%s", adress); Right?

Last edited by Havoc_101; Apr 10th, 2005 at 8:38 PM.
Havoc_101 is offline   Reply With Quote
Old Apr 10th, 2005, 9:41 PM   #4
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
do you have the necessary header files in place?
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Apr 10th, 2005, 9:43 PM   #5
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
Quote:
Originally Posted by Havoc_101
The gets() doesnt seem to work for me? It gives me an error on the stdio.h ? I am just replacing the scanf("%s", adress); to gets("%s", adress); Right?
It should actually just be:

  gets(adress);
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Apr 10th, 2005, 10:03 PM   #6
Havoc_101
Newbie
 
Join Date: Apr 2005
Posts: 19
Rep Power: 0 Havoc_101 is on a distinguished road
When i do it like that "gets(adress);" it just skips the question...
Havoc_101 is offline   Reply With Quote
Old Apr 11th, 2005, 5:07 PM   #7
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Don't use gets():

http://www.c-for-dummies.com/faq/
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:40 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC