Thread: Help needed (problem with basic C program)

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    8

    Exclamation Help needed (problem with basic C program)

    Hello all,

    The other day I was experimenting with C and I stumbled across a problem in my program, it's a basic one for inputting names into a two dimensional array. That's all. But for some reason I fail to see there are problems everywhere! It's a basic program so I hope it's easy for experienced programmers to spot my amateur mistake.

    Comments are integrated throughout the program.

    Thanks for the help


    Code below:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
      typedef char names[50];/*defining a new type of variable called name which is a string of 50 chars*/
      typedef int namespaces[50];/*defining another variable called namepaces which is an array of 50 rows*/
      char namearr[namespaces][names];/*declaring an array of 50 rows and each row consists of a string of 50 chars*/
      
      printf("please enter the names you want in the file (limit is 50 names)"); /*Prompting the user for names to input in the array with a max of 50*/
      for (int i=0;i<50;i++){                                                   /*nested for loops for inputing individual values into the array*/
     for(int j=0; j<50 ; j++)
     prinf("Type name entry %d\n",i);
     scanf(%s,&namearr[i][j]);}
     
    
    for (int i=0;i<50;i++){        /*nested for loops for echo-ing the inputted strings*/
     for(int j=0; j<50 ; j++)
     printf("The names you inputted are input[%d][%d]",i,j,namearr[i][j]);
    } 
      
      system("PAUSE");	
      return 0;
    }
    Last edited by EpicYuzer; 11-11-2010 at 12:18 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > typedef char names[50];/*defining a new type of variable called name which is a string of 50 chars*/
    > typedef int nmspaces[50];/*defining another variable called nmepaces which is an array of 50 rows*/
    > char namearr[namespaces][names];/*declaring an array of 50 rows and each row consists of a string of 50 chars*/

    This doesn't make any sense at all.

    For one thing, is the spelling mistake of namespaces as nmspaces something deliberate in the actual code you tried.

    Or is this some half-assed "as I remember it" code you just pulled from your ass to amuse us?
    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.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    8
    Quote Originally Posted by Salem View Post
    > typedef char names[50];/*defining a new type of variable called name which is a string of 50 chars*/
    > typedef int nmspaces[50];/*defining another variable called nmepaces which is an array of 50 rows*/
    > char namearr[namespaces][names];/*declaring an array of 50 rows and each row consists of a string of 50 chars*/

    This doesn't make any sense at all.

    For one thing, is the spelling mistake of namespaces as nmspaces something deliberate in the actual code you tried.

    Or is this some half-assed "as I remember it" code you just pulled from your ass to amuse us?
    No it's not I copied and pasted the code in but compiled it with everything as namespaces and then thought the "namespaces" was a conflicted keyword in C++ "namespace" so I changed everything to "nmsapces" and back again I just forgot to change the first one but by no means is that the problem of the program.

    Thanks for pointing out the post flaw, I will fix it now, but you could have stated it in a more sophisticated and in a less spazzy I know more than you do manner. I am by no means a professional programmer and am just grasping these ideas so I ask no more than being polite and kind help. If you're able to offer that help you are welcome to do so but this tone of yours doesn't "ammuse" me at all since I am NOT here to play with someone or waste time...

    شكراَ لك يا أخي أرجو أنني لم أزعجك بسؤالي التافه يا مؤدب...
    Last edited by EpicYuzer; 11-11-2010 at 12:27 AM.

  4. #4
    Registered User gaurav9991's Avatar
    Join Date
    Oct 2010
    Location
    Pune, Maharashtra, India
    Posts
    69
    you first tell us, what is your definition of "typedef"

    char namearr[namespaces][names];/*declaring an array of 50 rows and each row consists of a string of 50 chars*/
    array size should be a constant integer number, here "namespaces" and "names" are variable names

    scanf(%s,&namearr[i][j]);
    just open any book on 'c' and see syntax of scanf()

    printf("The names you inputted are input[%d][%d]",i,j,namearr[i][j]);
    2 access specifiers and 3 variable names ???

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Not really.

    It was just another code dump without explanation.
    Trivial to spot syntax errors mean you didn't even do the basics of making sure that it compiled.

    Why is this a problem?
    Because we end up spending at least a day and half a dozen messages just fixing (or guessing) whatever imaginary mistakes there are, only to be met with responses "Oh well, the real code isn't like that".

    And yes, I can use google translate as well, thanks for asking.

    Others can chip in, I've lost sympathy for your case.
    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.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    8
    Quote Originally Posted by Salem View Post
    Not really.

    It was just another code dump without explanation.
    Trivial to spot syntax errors mean you didn't even do the basics of making sure that it compiled.

    Why is this a problem?
    Because we end up spending at least a day and half a dozen messages just fixing (or guessing) whatever imaginary mistakes there are, only to be met with responses "Oh well, the real code isn't like that".

    And yes, I can use google translate as well, thanks for asking.

    Others can chip in, I've lost sympathy for your case.
    Oh really? I am really looking for your sympathy... Please please help. Realistically I couldn't be less concerned of who you are and whether I get your dear sypathy or not, because there are alot of programmers here that are willing to help without their noses up in the sky like you.

    It's a shame seeing your name was an arabic one I thought you'd understand arabic I wrote "sorry brother, I hope I haven't bothered you with my pathetic question oh polite one" but I bet google translate returned that too.

    I lost all respect for you and I don't think another keystroke reply of yours will result in one keystroke of mine so save your precious time you spend replying uselessly and go learn some lessons on humbleness and how to helpful towards others. The fact that you don't know everything after C is kind of shocking, huh?

  7. #7
    Registered User
    Join Date
    Nov 2010
    Posts
    8
    Quote Originally Posted by gaurav9991 View Post
    you first tell us, what is your definition of "typedef"



    array size should be a constant integer number, here "namespaces" and "names" are variable names



    just open any book on 'c' and see syntax of scanf()



    2 access specifiers and 3 variable names ???

    Thanks so muc for the kind reply! I see that you cannot use a variable in an array definition so do I just use namearr[namespaces[50]][names[50]];?

    scanf is used as follows

    scanf(%d, &int variable here);
    scanf(%c, &char variable here);
    scanf(%lf, &double variable here);

    but I dont understand how i will incorporate the array in there. I could work my way arround before scanf by:
    char inpt[50];
    namearr[i][j]=inpt[50];
    scanf(%s, inpt);

    is that true?


    and for the remaining printf i am completely clueless since the problem with array input in scanf correctly I am unsure of how I can do it with printf..

    Thanks for all your help.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by EpicYuzer View Post
    Thanks so muc for the kind reply! I see that you cannot use a variable in an array definition so do I just use namearr[namespaces[50]][names[50]];?
    Ummmm.... no that would be namearr[50][50];

    The typedefs are totally nonsensical in this case.

    but I dont understand how i will incorporate the array in there. I could work my way arround before scanf by:
    char inpt[50];
    namearr[i][j]=inpt[50];
    scanf(%s, inpt);
    If you are building an array of 50 strings of 50 characters each C will let you use...
    scanf("%s",namearr[i]) putting it directly into your array.

    Moreover you can't do string = string in C. You will need library functions like strcpy(), strcat() etc. to do that for you.


    and for the remaining printf i am completely clueless since the problem with array input in scanf correctly I am unsure of how I can do it with printf..
    Well... you're going to have to learn C syntax and how to look stuff up for yourself sometime...
    What does the C library documentation say about printf() ?
    Last edited by CommonTater; 11-11-2010 at 01:08 PM.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Posts
    8
    Quote Originally Posted by CommonTater View Post
    Ummmm.... no that would be namearr[50][50];

    The typedefs are totally nonsensical in this case.



    If you are building an array of 50 strings of 50 characters each C will let you use...
    scanf("%s",namearr[i]) putting it directly into your array.



    Well... you're going to have to learn to look stuff up for yourself sometime...
    What does the C library documentation say about printf() ?
    okay but what typpe would my array be? just char namearr[50][50]?
    So it would be of 50 rows each one containing a string of 50 chars?

    Since C lets me use scanf("%s", namearr[i]) to input the string in the ith row into my array then so should printf when i say printf("Entry number %d is %s",i,namearr[i]); ?

    printf is almost the same as scanf where to output a variable type you use %(symbol of that variable) but you can also output text.

    for example i want to output a string declared as char greeting[21] and inputted my code would be

    char greeting[20];
    printf("please input the greeting you want me to output on the screen,-no longer than 20 characters-\n");
    scanf("%s", greeting);
    printf("the string you inputted is %s\n", greeting);

    Thanks for the reply it's really helping

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by EpicYuzer View Post
    okay but what typpe would my array be? just char namearr[50][50]?
    So it would be of 50 rows each one containing a string of 50 chars?
    Yes... that is how 2 dimensional arrays are declared. They can be any type. Remember a string in C is simply an array of characters so the result is 50 x 50 character strings.

    Since C lets me use scanf("%s", namearr[i]) to input the string in the ith row into my array then so should printf when i say printf("Entry number %d is %s",i,namearr[i]); ?
    It should, as long as the array bounds in i are valid.


    printf is almost the same as scanf where to output a variable type you use %(symbol of that variable) but you can also output text.

    for example i want to output a string declared as char greeting[21] and inputted my code would be

    char greeting[20];
    printf("please input the greeting you want me to output on the screen,-no longer than 20 characters-\n");
    scanf("%s", greeting);
    printf("the string you inputted is %s\n", greeting);

    Thanks for the reply it's really helping
    Ok... now it's time to move this to the next level...

    1) Please learn how to use code tags when posting here... and use them.
    2) Please test your code... compile it and run it before asking questions about it.

    You will need these skills sometime... now is as good a time as any.

  11. #11
    Registered User
    Join Date
    Nov 2010
    Posts
    8
    Quote Originally Posted by CommonTater View Post
    Yes... that is how 2 dimensional arrays are declared. They can be any type. Remember a string in C is simply an array of characters so the result is 50 x 50 character strings.



    It should, as long as the array bounds in i are valid.




    Ok... now it's time to move this to the next level...

    1) Please learn how to use code tags when posting here... and use them.
    2) Please test your code... compile it and run it before asking questions about it.

    You will need these skills sometime... now is as good a time as any.
    Thanks so much for your feedback!

    The program compiled with the following code

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
     int i, j;
      char namearr[50][50];
      
      printf("please enter the names you want in the file (limit is 50 names)");
      
      for(i=0;i<50;i++)
      {
     print
     f("Type name entry %d\n",i);
     scanf("%s",&namearr[i]);
     }
     
    
    for(j=0;i<50;i++)
    {
     printf("Input %d is %s\n",i,namearr[i]);
    } 
      
      system("PAUSE");	
      return 0;
    }
    But it seems to always ask for strings even when I input '\0' or nothing at all. Haven't found a way around that yet.

    and another when I put a for loop as follows:

    Code:
    for(i=0;i<50;i++)
    it doesn't compile due to " 'for' loop initial declaration used outside C99 mode " error. I don't understand why...

    Thanks for the help

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
     
    for(j=0;i<50;i++)  //use i not j, 
    {
     printf("Input %d is %s\n",i,namearr[i]);
    }
    Should help some.

  13. #13
    Registered User
    Join Date
    Nov 2010
    Posts
    8
    Quote Originally Posted by Adak View Post
    Code:
     
    for(j=0;i<50;i++)  //use i not j, 
    {
     printf("Input %d is %s\n",i,namearr[i]);
    }
    Should help some.
    Yes thanks!


    The final successful code is:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
     int i,j;
      char namearr[50][50];
      
      printf("please enter the names you want in the file (limit is 50 names)");
      
      for(i=0;i<50;i++)
      {
     printf("Type name entry %d\n",i);
     scanf("%s",&namearr[i]);
     }
     
    
    for(j=0;j<50;j++)
    {
     printf("Input %d is %s\n",j,namearr[j]);
    } 
      
      system("PAUSE");	
      return 0;
    }
    But still I can't find a way to stop prompting the user for strings if he has entered some number of strings less than 50 and has no more to add.

    And still I don't understand why:

    Code:
    for(int i=0;i<50;i++)
    doesnt work or compile for that matter as a for loop and returns error mentioned earlier...

  14. #14
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Adak pointed out a nice logical error in your program.

    Interestingly enough though, I didn't get such a warning when I pasted your code into a file and compiled it (gcc -Wall), but I did get the following:

    test.c:14: error: format '%s' expects type 'char *', but argument 2 has type 'char (*)[50]'

    You only need namearr[i], which is the address of the start of the ith element of namearray, which is a char *, which is what scanf expects.

    The "'for' loop initial declaration used outside C99 mode" error usually means that you did something like
    Code:
    for (int i = 0; i < 50; i++)
    instead of

    Code:
    int i;
    for (i = 0; i < 50; i++)
    The former is not compliant with the C standard most compilers use while the latter is legit C/C++ pretty much everywhere.

    History: In 1999, they came up with the C99 standard, which allowed declaring your iterator inside the for loop, but since that standard had lots of other complicated stuff for compilers to implement (like handling imaginary numbers and type-agnostic math functions), so very few compilers ever fully implemented the standard. Thus, unless you specify something like --std=c99 (gcc), the compiler may complain about such declarations.

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by EpicYuzer View Post
    But still I can't find a way to stop prompting the user for strings if he has entered some number of strings less than 50 and has no more to add.
    Ok, here comes the "looking stuff up" part... Take a look at the stuff in your entries loop. Actually look them up in the C library documentation and study how they work... Does anything in that documentation suggest a way of ending the loop?

    Code:
    for(int i=0;i<50;i++)
    doesnt work or compile for that matter as a for loop and returns error mentioned earlier...
    That's because of the for (int i ... With the declaration inside the braces is a C-99 enhancement and you are not using a C-99 capable compiler.

    Yet another case for looking stuff up.

    Seriously... Do you know the 4 rules of computer bliss?

    1) Read the effing screen
    2) Read the effing help file
    3) Read the effing manual
    4) Ok, now you can ask your question.

    Seriously... This has been around for years and years... Ever heard RTFM... well, that's were it comes from.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-16-2009, 06:00 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. Problem with basic encryption program.
    By mmongoose in forum C++ Programming
    Replies: 5
    Last Post: 08-27-2005, 04:41 AM
  4. Problem with simple XOR program
    By spike_ in forum C++ Programming
    Replies: 8
    Last Post: 08-17-2005, 12:09 AM
  5. Replies: 5
    Last Post: 12-03-2003, 05:47 PM

Tags for this Thread