Having trouble with a simple program......

This is a discussion on Having trouble with a simple program...... within the C Programming forums, part of the General Programming Boards category; I've been working on this program that should be simple for the last hour or so with no luck. The ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    7

    Having trouble with a simple program......

    I've been working on this program that should be simple for the last hour or so with no luck.

    The question reads :

    Write a function to read in a line of text (use getchar() routine) and display only the capital letters contained in the text. You can declare and use an array to store the text and a pointer to access the array.

    What i've attemped so far is :

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    
    int main(){
    
    char line_of_text[100], *charPtr;
    
    charPtr = line_of_text[0];
    
    while (charPtr = getchar());
    
    if ((charPtr >= 'A') && (CharPtr =< 'Z'))
    
    putchar();
    However DevC++ is telling me that theres a problem with the charPtr = getchar() part. I'm also at a loss how to keep the loop running when it comes across a non capital letter , i think the way mine is itll simply stop the loop so i'm trying to think of something to put in the else part thatll cause it to go through the loop again. I'm sure this is a really easy problem for most people but i am utterly rubbish at programming and cant figure it out

  2. #2
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    There are some fundemental problems with your code.

    Code:
    charPtr = getchar();
    charPtr is a char*. getchar returns char.

    Code:
    while(...);
    Are you sure about that final semicolon there?

    Code:
    putchar();
    You must putchar something.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    11
    The below line of code will infinitely waiting by accepting the characters.
    This is wrong
    Code:
    while (charPtr = getchar());

    Probably , you may be looking when the user hits an enter key then that is the end of the string.
    [code]
    while (charPtr = getchar())
    {
    line_of_text[i++] = charPtr;
    if(charPtr == '\n')
    break;
    }
    [code/]

    Next Step you need to do is filter the Capital letters from the String.

  4. #4
    Registered User
    Join Date
    Jul 2005
    Posts
    11
    Sorry for the Code Tags


    Probably , you may be looking when the user hits an enter key then that is the end of the string.
    Code:
    while (charPtr = getchar())
    {
    line_of_text[i++] = charPtr;
    if(charPtr == '\n')
    break;
    }
    Next Step you need to do is filter the Capital letters from the String

  5. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    7
    This program is highly confusing me so i figured i'd try and get the basic operation of getchar and putchar down. I just threw this code out, is this the correct way to use them ?

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    
    int main(){
    
    
    
    int numChar;
    
    while ((numChar = getchar()) != '\n')
    {
    putchar(numChar);
    }
    
    
    return 0;
    
    }
    Whats confusing me is the bit at the end of the question that says "You can declare and use an array to store the text and a pointer to access the array" I gather from that that i need to input my line into an array, using a pointer to access it so i can filter out the capital letters, but when i try to use the getchar routine on the pointer it doesnt work, so i'm guessing theres another way.

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,171
    Using an array to do this would be poor programming. There's no need for the additional overhead.

    Your use of getchar() and putchar() are correct. I would just add one line inside your while() loop that only calls putchar() if numChar is a capital letter. (use isupper()).
    Last edited by itsme86; 08-09-2006 at 12:37 PM.
    If you understand what you're doing, you're not learning anything.

  7. #7
    Registered User
    Join Date
    Aug 2006
    Posts
    7
    Yeah the fact it said you could use an array and a pointer was what was really annoying me.

    I've put in another line but i still think theres something else needed, cause im not sure what would happen if a non-capital letter came up. This is my current code

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    
    int main(){
    
    
    
    int numChar;
    
    while ((numChar = getchar()) != '\n')
    {
    if (isupper(numChar))
    putchar(numChar);
    }
    
    
    return 0;
    
    }
    I think i need an else which will make the while loop start again, but i'm not sure if thats the right way to go about it. Thanks for your help.

  8. #8
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,751
    Usually a question like that can be answered by compiling and running the code.
    In this case if numChar is a small letter, the if is false and nothing will be put until the next iteration.

  9. #9
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,171
    Your code looks right to me. Next step: testing

    Run your program with various inputs and see if you get the expected results.
    If you understand what you're doing, you're not learning anything.

  10. #10
    Registered User
    Join Date
    Aug 2006
    Posts
    9

    OMG! its in K&R book!

    // look
    int c;
    while ((c = getchar()) > 0 && c != EOF) {
    if (c >= 'A' && c <= 'Z')
    putchar (c);
    if (c == ' ' || c =='\t')
    putchar (c);
    }

    /* - you read stdin with getchar into c variable (char c is enough for me but D.R. said int)
    * - then check if you have reached the End Of File (quite lazy approach)
    * - then you check okay is this really a capital letter? (it could be a more complicated
    * function call when u grow up)
    * - and then fine! print it out with putchar()
    * (putchar does really require an argument as in put what?)
    * - and for the dessert u might want to leave the formatting characters so
    * u dont make new words (changing facts is really not a good idea);
    */

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 01:27 PM
  2. [Help] Simple Array/Pointer Program
    By sandwater in forum C Programming
    Replies: 3
    Last Post: 03-30-2007, 03:42 PM
  3. simple silly program
    By verbity in forum C Programming
    Replies: 5
    Last Post: 12-19-2006, 06:06 PM
  4. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 08:52 AM
  5. A VERY simple program is confusing me..HELP PLEASE!
    By BBain80 in forum C++ Programming
    Replies: 3
    Last Post: 02-27-2002, 04:42 PM

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