Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,210 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 911 people online right now. Registration is fast and FREE... Join Now!




pls correct me! got some problems

 
Reply to this topicStart new topic

pls correct me! got some problems

zattara13
post 2 Apr, 2008 - 11:23 AM
Post #1


New D.I.C Head

*
Joined: 27 Mar, 2008
Posts: 26

can u see y the output of this program is very wrong? imagine u input a name and a telephone number only, and u tell the program to input s1, s2 and s3.. it will give u a lot of symbols after the telephone number! :S:S any help?

and also how can i then get the numbers to be saved in an array? how can i do that? 10x

CODE


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

int i, j;

int main (void)
{
    
    char* s[2][100];
    char* n[2][100];
    
    //char* s = "[Gordon Camilleri ][5432][, ]                         [13] [th may][2006]    [ Street, Fgura FGR][123][; ][21212121]
    //char* s = "[Gordon Camilleri ][5432][, Borg Street, Fgura FGR][123][; ]    [21212121]
    //              s1                 s2    s3                        s4   s5      s6         s7                  s8   s9  s10    
    
    char s1[100],s2[100],s3[100],s4[100],s5[100],s6[100],s7[100],s8[100],s9[100],s10[100];
    int x;

    for(i=0;i<2;i++)
    {
        printf ("Enter Name, Address and Phone : ");
        gets(s[i]);
        printf("\n");
    }
    
    for(i=0;i<2;i++)
    {
        sscanf(s[i], "%[^0123456789]%[0123456789]%[^0123456789]%[0123456789]%[^0123456789]%[0123456789]%[^0123456789]%[0123456789]%[^0123456789]%[0123456789]",
                          s1           ,s2          ,s3           ,s4          ,s5           ,s6          ,s7           ,s8          ,s9           ,s10);

        //printf("NAME\t\t\tTELEPHONE NUMBER\n");
        printf("%s\n%s\n%s\n\n", s1,s2,s3);

    }

    return (0);
}


User is offlineProfile CardPM

Go to the top of the page

Martyr2
post 2 Apr, 2008 - 01:35 PM
Post #2


Programming Theoretician

Group Icon
Joined: 18 Apr, 2007
Posts: 5,012



Thanked 171 times

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions


Try using....

cpp

char s[2][100];
char n[2][100];


This should allow you to input data from the user without a bunch of symbols popping up. Then it will be up to you as to how you want to format that data in sscanf. Keep in mind that the example strings you have there appear to be bigger than 100.

smile.gif

User is offlineProfile CardPM

Go to the top of the page

Moezzie
post 2 Apr, 2008 - 01:46 PM
Post #3


New D.I.C Head

*
Joined: 25 Nov, 2007
Posts: 44


My Contributions


QUOTE(zattara13 @ 2 Apr, 2008 - 12:23 PM) *

it will give u a lot of symbols after the telephone number! :S:S any help?


I actually had the same problem with one of my applications today, i have no idea what triggered it.

I read three characters from a file through read(), but instead i got nine, six of them beeing wierd characters that changed on every debug.

Anyways, what i ended up doing was resizing the string to the exact length of my input.
So if you want a 10 digit number, maybe you should try resizing your string to 10 characters.

Try using this funktion:
myString.resize(size);
You can read about it on this site

This probably isnt a really professional aproach, but at least i worked. You can try it out if you want to.
Good luck!
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/21/08 07:59PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month