d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Need Help In A Basic C Name Proggy
Add Reply New Topic New Poll
Member
Posts: 51
Joined: Feb 7 2013
Gold: 0.00
Mar 16 2013 10:26am
my assignment:
Create a program that allows a user to enter up to five names
of friends. Use a two-dimensional array to store the friends’
names. After each name is entered, the user should have the
option to enter another name or print out a report that shows
each name entered thus far.

my problem :
it quits after 5 names are entered
the printout for names entered looks...weird



my code:
Code
#include <stdio.h>
#include <stdlib.h>
void menu(void);

int menu_op = 1;

int main(void) {
   char names[5][30];
   
   printf("\tName Entry (Enter up to 5 names)\n\n");
   
   int i = 0;
   for (; i < 5; i++) {
       if (menu_op == 1) {
           printf("Enter a name: ");
           scanf("%s", names[i]);
       }
       
       if (menu_op == 2) {
           int j = 0;
           printf("\nNames that have been entered:");
           for (; j < 5; j++)
               printf("\n\t%s", names[j]);
       }
       
       if (menu_op == 3)
           break;
       
       menu();
   }
   //printf("\n\t%s", &names[0]);
   int j = 0;
   printf("\nNames that have been entered:");
   for (; j < 5; j++)
       printf("\n\t%s", names[j]);
   
   return 0;
}

void menu(void) {
   printf("\nMenu:\n");
   printf("\n1 Enter another name");
   printf("\n2 Show names entered");
   printf("\n3 Quit");
   printf("\nEnter menu option: ");
   scanf("%d", &menu_op);
   system ("pause");
}
Member
Posts: 1,358
Joined: Dec 30 2012
Gold: 0.10
Mar 16 2013 12:48pm
Nvm

Why would you need a 2d array to store a list of friend names?

But you need to get input for the menu option after every time they enter a name.

Scanf("%i", &menu_op);



This post was edited by SelfTaught on Mar 16 2013 12:57pm
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Mar 16 2013 01:03pm
Quote (SelfTaught @ Mar 16 2013 02:48pm)
Nvm

Why would you need a 2d array to store a list of friend names?

But you need to get input for the menu option after every time they enter a name.

Scanf("%i", &menu_op);


he doesnt even call menu() thats why his menu_op is unset.

also why do you need a global variable for this. just return an integer from the function.
Member
Posts: 15,988
Joined: Nov 12 2005
Gold: 4,399.00
Mar 16 2013 01:35pm
Shortly, in C language all char arrays are signing to random values after running.
So, when you're trying to print it, it will print all those random values too. You can't check it for NULL, because it's feature of C language.
Example - http://stackoverflow.com/questions/12676804/printing-char-arrays-in-c

It seems like you have to end array with value '\0' manually after each writing to array.
I mean to use something like

Quote
...
          printf("Enter a name: ");
          scanf("%s", names[i]);
          if (i!=5)
            names[i+1][0]='\0';

...

in both if statements (at the end and in loop)
It will add \0 char to first place of next row after each entering of name.

+ you have to add new if statement inside loop within inside menu_op == 2.
Quote
...
if (menu_op == 2) {
          int j = 0;
          printf("\nNames that have been entered:");
          for (; j < 5; j++)
              if (names[j][0] != '\0')
              printf("\n\t%s", names[j]);
              else break;
...

it will simply stop printing names after finding this \0 char in line.

btw, \0 is equal of NULL, but for char and strings. down
By whatever reason, in C language CPU gives random values after allocating memory for char and string valuebles.

This post was edited by dimon222 on Mar 16 2013 01:51pm
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Mar 16 2013 01:49pm
Quote (dimon222 @ Mar 16 2013 03:35pm)
btw, \0 is equal of NULL, but for char and strings.


this is wrong. \0 is a nullbyte which is a zero. NULL is a typedef that means "nothing" and does not have a value and is used for memory handling. they are two different things.

Quote
By whatever reason, in C language CPU gives random values after allocating memory for char and string valuebles.


this on the other hand is true. thats why i always initiate my char arrays via

Code
char something[256] = {0};


forcing the entire array to be initiated to null bytes. an alternitve is to run memset() or bzero() depending on your operating platform on the array.

This post was edited by AbDuCt on Mar 16 2013 01:51pm
Member
Posts: 15,988
Joined: Nov 12 2005
Gold: 4,399.00
Mar 16 2013 01:49pm
Quote (AbDuCt @ 16 Mar 2013 23:49)
this is wrong. \0 is a nullbyte which is a zero. NULL is a typedef that means "nothing" and does not have a value and is used for memory handling. they are two different things.

Thanks for correction. :)
Quote (AbDuCt @ 16 Mar 2013 23:49)
Code
char something[256] = {0};


forcing the entire array to be initiated to null bytes.

I tried this, but I didn't get it to work with two-dimensional arrays.

This post was edited by dimon222 on Mar 16 2013 01:51pm
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Mar 16 2013 01:52pm
Quote (dimon222 @ Mar 16 2013 03:49pm)
Thanks for correction.  :)


np. added more clarification to help they be a better programmer and to relieve headaches in the future via initiating arrays as they are declared or using memory functions to zero out arrays.

Quote (dimon222 @ Mar 16 2013 03:49pm)
Thanks for correction.  :)

I tried this, but I didn't get it to work with two-dimensional arrays.


do {0,0} i think it is.

This post was edited by AbDuCt on Mar 16 2013 01:53pm
Member
Posts: 51
Joined: Feb 7 2013
Gold: 0.00
Mar 16 2013 07:58pm
Quote (SelfTaught @ Mar 16 2013 01:48pm)
Nvm

Why would you need a 2d array to store a list of friend names?

But you need to get input for the menu option after every time they enter a name.

Scanf("%i", &menu_op);


i've asked you not to post on my topics please respect my wishes
Member
Posts: 51
Joined: Feb 7 2013
Gold: 0.00
Mar 16 2013 08:03pm
Quote (AbDuCt @ Mar 16 2013 02:52pm)
np. added more clarification to help they be a better programmer and to relieve headaches in the future via initiating arrays as they are declared or using memory functions to zero out arrays.



do {0,0} i think it is.


and you've certainly helped me majorly improve my programming >,<
however my code works as intended it's just that it displays eg
Abduct (name)
(

(
8(
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll