Thread: Help with strings and numbers ;[

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

    Help with strings and numbers ;[

    Hi, first, I'm from Brasil, so sorry for any eventual english errors ;p
    K =D Here's the deal, I need to build a programa that read 20 names, and 3 notes (ratings ? scores ?) (like 6,0 , 4,5 , 7,0 ...) and made a average rating..., and if I write END in a name, the programa stops collecting names and notes...

    Until now I did this:

    Code:
    for (CONTN=0;CONTN<3;CONTN++)
    {
    printf ("\nEnter a name:");
    gets (NAME[CONTN]);
    for (CONTY=0;CONTY<3;CONTY++)
    {
    printf ("\nNote %d:",CONTY);
    scanf ("%f",&NOTE[CONTN][CONTY]);
    SOMA = SOMA+NOTE[CONTN][CONTY];
    }
    AVG = SOMA/3;
    NOTE[CONTN][3] = AVG ;
    }
    printf ("Name, Avg");
    for (CONTN=0;CONTN<2;CONTN++)
    {
    printf ("\n%s %f",NAME[CONTN],AVG);
    }
    }
    Its suposed to get the name, and 3 notes, so made a average note , and after all, print this on the screen...

    Buuuttt... it's all wrong =/ I really dont know what is wrong... And dont know how to do that if-write-END-stops... if someone can help-me, I will be very pleased...

    Thanks =]

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Code:
    for (CONTN=0;CONTN<3;CONTN++)
    This runs 3 times so for 20 names change 3 to 20.
    When a name is entered compare it with "END" and if they are equal use break to exit the for loop.
    Code:
    if (!strcmp(NAME[CONTN], "END"))
       break;
    Code:
    SOMA = SOMA+NOTE[CONTN][CONTY];
    make sure that SOMA is set to zero before entering the 3 scores as it will have the total from the last names.

    You should also count the number of names that were entered before END and use this count in the final for loop that displays averages.
    Last edited by Quantum1024; 05-16-2006 at 06:36 PM.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    5
    Hum... thanks for the code to compare the END.

    Did what you said... When I start the program it already jumps for the Note 0 part.. Like:

    Code:
    Enter a name:
    Note 0: [so if I write, the name appears here]
    
    Note 1:
    Note 2:
    Enter a name: [and write another name...]
    Note 0:
    ... Basicly its not geting the Notes (I cant insert them =p) ... And just ignoring the FOR I did for the notes (where it should be NOTE[0][0], NOTE [0][1], etc =[



    Thanks again =]

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    So the program just exists after you entered the first name? Post the complete program.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    5
    Here it is:

    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;
    CONTN = 0;
    CONTY = 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],MEDIA);
    }
    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)

    Thanks... again ;[

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    903
    Why is this in the C++ forum ?

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    yes it belongs in c.

    change the part the get the name to this
    Code:
    printf ("\nNota %d:",CONTY);
    char Line[31];
    gets(Line);
    sscanf(Line, "%f", &NOTA[CONTN][CONTY]);
    scanf must be leaving the newline charactor which the next call to gets reads in instead of the next line.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

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