Thread: Help with strings and numbers ;[

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    5

    Help with strings and numbers ;[

    Hello, hum... I posted it in the wrong forum... so posting it here now...
    Here is the deal: I must do a program that read a name, and 3 notes (rating ? Score ? =p) and made a average note of these 3...] And if you writes END ("fim" in this case) it should stop...

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    
    int main ()
    {
    float NOTA[3][4],MEDIA,SOMA;
    int CONTN,CONTY,CONTA;
    char NOME[3][31];
    SOMA = 0;
    CONTA = 0;
    for (CONTN=0;CONTN<3;CONTN++)
    {
    printf ("\nDigite o nome:");
    gets (NOME[CONTN]);
    CONTA = CONTA++;
    if (!strcmp(NOME[CONTN], "fim"))
     break;
    for (CONTY=0;CONTY<3;CONTY++)
    {
    printf ("\nNota %d:",CONTY);
    scanf ("%f",&NOTA[CONTN][CONTY]);
    SOMA = SOMA+NOTA[CONTN][CONTY];
    }
    MEDIA = SOMA/3;
    NOTA[CONTN][3] = MEDIA;
    }
    printf ("Nome, Media");
    for (CONTN=0;CONTN<CONTA;CONTN++)
    {
    printf ("\n%s %.2f",NOME[CONTN],NOTA[CONTN][3]);
    }
    getch();
    }
    The first name apparently goes ok... after that it goes wrong =o

    BTW: I'm just testing with 3 names first...
    BTW²: I'm using portugues words.. (Nome = Name) (Media = Average)

    Anyone have any ideia that can help-me ? =p What is wrong ?

    Thanks... =]

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    31
    Your code is hard to read. It's pretty conventional to use tabs for things like if statements, loops, etc:
    example:
    Code:
    for (i = 0; i < 10; ++i) {
        scanf("%s", str);
    }
    You'd probably have a better chance at getting help if you indented and it'd make it easy for you in the future to take a look at your own code.

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    167
    It seems you needed to refresh the input buffer. I did it with

    fflush(stdin)

    I made some other changes because it wasn't running correctly. SOMA needed to be reinitialized to zero within the loop.. Also you were not incrementing CONTA correctly. It should be CONTA++ instead of CONTA = CONTA++.

    Comments are in the code where the errors are.

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    
    int main ()
    {
    	float NOTA[3][4],MEDIA,SOMA;
    	int CONTN,CONTY,CONTA;
    	char NOME[3][31];
    	
    	CONTA = 0;
    
    	for (CONTN=0;CONTN<3;CONTN++)
    	{
    
    		// SOMA needs to be 
    		// reinitialized to zero 
    
    		SOMA = 0;
    		printf ("\nDigite o nome:");
    
    		// Input buffer needed to be 
    		// flushed it seems 
    
    		fflush(stdin);
    		gets (NOME[CONTN]);
    
    		//CONTA = CONTA++;
    		CONTA++;
    
    		if (!strcmp(NOME[CONTN], "fim"))
     			break;
    
    		for (CONTY=0;CONTY<3;CONTY++)
    		{
    			printf ("\nNota %d:",CONTY);
    			fflush(stdin);
    			scanf ("%f",&NOTA[CONTN][CONTY]);
    			SOMA = SOMA+NOTA[CONTN][CONTY];
    		}
    
    		MEDIA = SOMA/3;
    		NOTA[CONTN][3] = MEDIA;
    
    	}
    
    	printf ("Nome, Media");
    	for (CONTN=0;CONTN<CONTA;CONTN++)
    	{
    		printf ("\n%s %.2f",NOME[CONTN],NOTA[CONTN][3]);
    	}
    	_getch();
    	return 0; 
    }
    Last edited by silk.odyssey; 05-16-2006 at 09:30 PM.
    silk.odyssey

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    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.*

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    5
    =o wow... Thanks silk.odyssey ... I love you man

    I didnt about that flush thing =o Very nice...

    Thanks a lot

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Seems that someone else has been poisoned by fflush(stdin) and gets()
    Oh well, time will surely tell....
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Quote Originally Posted by majindox
    =o wow... Thanks silk.odyssey ... I love you man
    =( wow... Did you even look at Dave's post?

    Quote Originally Posted by majindox
    I didnt about that flush thing =o Very nice...
    and dangerous... and wrong. Fergitaboutit now!!!!
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  8. #8
    Registered User
    Join Date
    Dec 2003
    Posts
    167
    My apologies for the fflush thing, I wasn't aware of the problem. Any good solutions?
    silk.odyssey

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    A couple. Your compiler might have a fuction that really cleans the input buffer, but you have to find it first, and it's not portable. It's probably in stdio.h.

    A more portable way to do this is a while loop with getchar() that eats everything up. It doesn't work as well as your compiler's function but it gets the job done.
    Code:
    int c; 
    while((c=getchar()) != '\n' && c != EOF);
    It's in the FAQ.

  10. #10
    Registered User
    Join Date
    Dec 2003
    Posts
    167
    I've found that solution. The only problem is that it requires pressing enter when the buffer doesn't need to be flushed. Is there a way of determining before hand if a flush is necesary? In the mean time I would try to find out if the compilers I have support it. I have using msvc2005 and pellesc by the way if anyone knows a function that would do the job.
    silk.odyssey

  11. #11
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    I like to use a careful combination of fgets and the flushing
    method just mentioned - fgets leaves characters in the buffer
    if the sting entered is too large. The natural thing would be to
    flush these, but the getchar method will cause a user to have to
    press enter if there was no data to flush. My solution is this
    function which I coded:

    Code:
    void strget (char *str, int size, FILE *ip)
    {
    	char *pointer;
    	int clear;
    
    	fgets (str, size, ip);
    
    	if ((pointer = strchr(str, '\n')) != NULL)	/*if newline is present*/
    		*pointer = '\0';			/*change it to \0*/
    
    	/*otherwise there is at least one character in the buffer, so flush it*/
    	else while ((clear = getchar ()) != '\n' && (clear != EOF));
    }
    [edit]
    oops, forgot to mention this needs string.h for strchr, although
    you could write some code to get around it I'm sure. Also,
    see my sig, it appears that you have missed what people such
    as Salem and Dave_Sinkula are saying about gets()
    Last edited by Richie T; 05-17-2006 at 12:18 PM.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  12. #12
    Registered User
    Join Date
    Dec 2003
    Posts
    167
    Thanks a lot.
    silk.odyssey

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 12-21-2007, 01:38 PM
  2. strings and numbers
    By jrahhali in forum C++ Programming
    Replies: 5
    Last Post: 09-23-2003, 12:24 PM
  3. strings and numbers
    By Granger9 in forum C Programming
    Replies: 3
    Last Post: 09-10-2002, 04:11 PM
  4. Replies: 13
    Last Post: 01-22-2002, 04:38 AM
  5. File Access with Strings and Numbers
    By Mace in forum C Programming
    Replies: 3
    Last Post: 12-01-2001, 04:07 AM