0

So I've got an assignment for class. The point is to read in characters to a string. If there are 7, print the 7. If there are 10, print the 10. Less than or more than, print an error.

It is also supposed to convert keyboard characters into their associated number on a dialpad.

Doesn't work though and everytime I load netbeans it gives something different..

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>



int main(int argc, char** argv) {

    char buffer [10];
    int count=0, badline=0, nextchar;



    while(count < 10) {

        nextchar = getchar();

        if (isspace(nextchar) && nextchar !='\n') ;

        /*else if (badline==1 || count>10){
                badline=1;
            }*/

        else if (isalnum(nextchar)) {

            /*if (badline==1 || count>10) {
                badline=1;
                /*break;*/


            if (count <= 10 || count >= 7)
                buffer[count] = nextchar;
                ++count;
                /*break;*/
                continue;
            }


        else if (isalpha(nextchar)) {
            nextchar = tolower(nextchar);

            /*if (badline==1 || count>10) {
                badline=1;
               /*break;*/

            switch(nextchar)
            {
                case 'a': case 'b': case 'c':
                    nextchar = 2;
                    break;
                case 'd': case 'e': case 'f':
                    nextchar = 3;
                    break;
                case 'g': case 'h': case 'i':
                    nextchar = 4;
                    break;
                case 'j': case 'k': case 'l':
                    nextchar = 5;
                    break;
                case 'm': case 'n': case 'o':
                    nextchar = 6;
                    break;
                case 'p': case 'q': case 'r': case 's':
                    nextchar = 7;
                    break;
                case 't': case 'u': case'v':
                    nextchar = 8;
                    break;
                case 'w': case 'x': case 'y': case 'z':
                    nextchar = 9;
                    break;
                    }


                     buffer[count]=nextchar;
                            ++count;
                            /*break;*/
                            continue;
                    }


        else if (nextchar == '\n' || nextchar == EOF) {
            if (badline==1 || count>10)
                printf("Please enter acceptable input\n");
            else
                printf("Good line\n");
                printf("%s\n", buffer);
                badline = 0 ;
                memset(buffer, 0, 10);
            if (nextchar == EOF)
                /*return(EXIT_SUCCESS);*/
                badline = 0 ;
                count = 0 ;
                /*buffer[0] = '\0';*/
                memset(buffer, 0, 10);

        }

        else
            badline = 1 ;


    }

    printf("Please limit to 10\n");
    return nextchar;








    /*return (EXIT_SUCCESS);*/

}
1
Contributor
1
Reply
2
Views
7 Years
Discussion Span
Last Post by AnkhUNC
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.