C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-04-2009, 11:34 PM   #1
Registered User
 
Join Date: Sep 2009
Posts: 7
How to use getchar/gets to store chars in an array, all upper case, in a set amount?

Hello there. This is just part of error checking in a much larger program, where I have to use only getchar (if even) / gets to manipulate the array to only store uppercase A-Z characters, with min number of characters being 2, max 10. I guess I'm still not comfortable using these functions. No string library functions may be use. What I have so far is below. Any help would be highly appreciated, thanks in advance.

Code:
#include <stdlib.h>
#include <stdio.h>

int main()
{
	char string[11]; /* gets will replace newline with null, so don't have to initialize
						string[10] to null*/
	int iochar;


	printf("Input string with uppercase A-Z characters, min 2 max 10:\n");
	
	while(iochar = getchar())
	{
		if(iochar >= 'A' && iochar <= 'Z')
		{
		gets(string);
		}
	}


	puts(string); /* testing string */

}
What I have above doesn't do anything, I was just messing around with getchar and such. I tried to set the while loop as while(iochar = getchar() != '\n'), but that gave me some erroneous results. I've also tried setting an if statement to check for lowercase characters, and a printf call to tell the user that the string can't be lowercase, but it would be print that statement (can't be lowercase) every time a single lower case is found. This is just one part of my program that I can't seem to figure out...
RandomEvil is offline   Reply With Quote
Old 11-05-2009, 12:30 AM   #2
cph
Registered User
 
cph's Avatar
 
Join Date: Sep 2008
Location: Indonesia
Posts: 27
suggestion
Code:
#include <stdio.h>

int main (void)
{
    int       cnt = 0;
    const int max = 10;
    char      buf[11];
    int       ch;

    do {
        ch = getchar();
        if (ch >= 'A' && ch <= 'Z') {
            buf[cnt] = (char)ch;
            ++cnt;
        }
        if (ch == '\n') {
            break;
        }
    } while (cnt < max);
    buf[cnt] = '\0';
    puts(buf);

    return(0);
}
cph is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Format Precision & Scale mattnewtoc C Programming 1 09-16-2008 10:34 AM
Array Help silverback011 C Programming 7 08-19-2006 09:58 PM
Changing bkgrnd color of Child windows cMADsc Windows Programming 11 09-10-2002 11:21 PM
Contest Results - May 27, 2002 ygfperson A Brief History of Cprogramming.com 18 06-18-2002 01:27 PM
rand() serious C Programming 8 02-15-2002 02:07 AM


All times are GMT -6. The time now is 03:45 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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