Character to array (Noobie Q)

This is a discussion on Character to array (Noobie Q) within the C Programming forums, part of the General Programming Boards category; It seems as if someone else on here is doing the exact same assignment as me...haha...well...I have some different problems. ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    35

    Character to array (Noobie Q)

    It seems as if someone else on here is doing the exact same assignment as me...haha...well...I have some different problems.

    If you run this, you see that, after you enter the word, you have to hit <return> 10 times for the next "please enter your ## word" to come up. Its obviously effecting my results if you keep going through the program. (I don't know why this is). On top of that I don't want my words to exceed 9 characters, and I can't really test that yet because of my first bug.

    The code with the substitution of \n to \0 was given by our prof, because we obviously have to use strings in our program. I am not sure if I placed it in my program correctly however, it makes sence. For every time 'b' increases once, 'c' will increase 9 times, and each time assigning a character to "word[b][c]" until it reaches the new line character.

    (However we/nor I, have learned strings yet, so Im not trying to worry about that right now, Id just like to get the basic input and output down, before any sorting w/ strings).

    ex. of what code should do

    Enter ## word : FrOG
    etc...

    Unsorted code:
    fRoggoRf


    Code:
    #include <stdio.h>
    #include <string.h>
    
    char word[10][19];
    int a;
    int b;
    int c;
    int d;
    
    main (){
    
    printf("Enter words no longer than 9 characters\n");
    for (a=0; a<10; a++)
    {
    	if (a==0)
    		printf("Enter your 1st word:");
    	else if (a==1)
    		printf("Enter your 2nd word:");
    	else if (a==2)
    		printf("Enter your 3rd word:");
    	else
    		printf("Enter your &#37;dth word:", a+1);
    for (b=0; b<10; b++)
    	for (c=0; c<10; c++)
    	{
    		word[b][c]=fgetc(stdin);
    			if (word[b][c]=='\n')
    			{
    				word[b][c]='\0';
    				break;
    			}
    
    	if (c>=10)
    		printf("You have entered a word more than 9 characters\n");
                    /*goto A*/
    
    
    	}
    
    }
    printf("\n");
    printf("Unsorted Input: \n");
    printf("\n");
    for (b=0; b<10; b++)
    {
    	for(c=0; c<10; c++)
    	{
    		d=word[b][c];
    		if (d>=65 && d<=91)
    			d=d+32;
    		else if (d>=97 && d<=124)
    			d=d-32;
    		printf("%c",d);
    	}
    	for(c=10; c<=20; c++)
    	{
    		d=word[b][c];
    		if (d>=65 && d<=91)
    				d=d+32;
    		else if (d>=97 && d<=124)
    				d=d-32;
    		printf("%c",d);
    	}
    }
    return 0;
    }

    I like finding the errors, so if its a simple syntax error, just give me the line # and ill try to figure it out. But if its all mashed up, well then any adivce would be very helpful. Thank you.
    Last edited by Iconate; 02-27-2008 at 11:44 AM.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    First, main should return int.
    And then working some on the indentation doesn't hurt. http://cpwiki.sf.net/Indentation
    Reading strings with fgetc is just more trouble that it's worth if you ask me. It's better to just use fgets.
    Then you should use "magic numbers" such as 65 or 91. Use proper character constants such as 'a' and 'z' or such.
    Though you might need to make your array bigger to hold a little bigger string, or you'd have to deal with issues when fgets can't read the whole string.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    35
    Quote Originally Posted by Elysia View Post
    First, main should return int.
    And then working some on the indentation doesn't hurt. http://cpwiki.sf.net/Indentation
    Reading strings with fgetc is just more trouble that it's worth if you ask me. It's better to just use fgets.
    Then you should use "magic numbers" such as 65 or 91. Use proper character constants such as 'a' and 'z' or such.
    Though you might need to make your array bigger to hold a little bigger string, or you'd have to deal with issues when fgets can't read the whole string.
    Well my array is 19 character long, and if I limit the user to only enter 9 characters then when you double the word, its 18, plus the nul character right?

    *Fixed the "int main()" however it still didn't effect my spacing problem

    And if I use fgets, how do you notify the user if they have entered a word longer than 9 characters. I can only see how to do that with fgetc though

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    Quote Originally Posted by Iconate View Post
    Well my array is 19 character long, and if I limit the user to only enter 9 characters then when you double the word, its 18, plus the nul character right?
    Yes, but if the user enters a long word or something than 19 chars?
    I'm afraid it might leave data in the buffer. But it might not really matter. It doesn't have to be a perfect program.

    Quote Originally Posted by Iconate View Post
    And if I use fgets, how do you notify the user if they have entered a word longer than 9 characters. I can only see how to do that with fgetc though
    This one is simple!
    Just use strlen to check the length of the string. If it's > 9, then the user entered a too long string!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    35
    Quote Originally Posted by Elysia View Post
    Yes, but if the user enters a long word or something than 19 chars?
    I'm afraid it might leave data in the buffer. But it might not really matter. It doesn't have to be a perfect program.


    This one is simple!
    Just use strlen to check the length of the string. If it's > 9, then the user entered a too long string!
    haha, wow that sounds much easier. So is fgetc whats causing the huge spaces in my program? I still wanna be able to fix that some how, just so I know where I screwed up.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    Quote Originally Posted by Iconate View Post
    haha, wow that sounds much easier. So is fgetc whats causing the huge spaces in my program? I still wanna be able to fix that some how, just so I know where I screwed up.
    Well, the obvious flaw is that fgetc will ask for input, then read all the input, then ask for input again so it reads everything. This is due to your loop. You can, of course, manually break it, but I think it would be better and easier if you just used fgets to read the word instead.
    No more spaces and stuff. I hope.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    35
    Well our lesson on strings and #include <string.h> functions is tomrrow so ill try incorporating it after then, thanks a bunch

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Character Array comparison
    By magda3227 in forum C Programming
    Replies: 7
    Last Post: 07-09-2008, 08:36 AM
  2. Replies: 7
    Last Post: 05-11-2008, 10:57 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  4. two dimensional character array
    By feuerraeder in forum C Programming
    Replies: 4
    Last Post: 11-22-2002, 07:59 AM
  5. Array of Character Arrays
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 02-09-2002, 05:07 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21