Thread: scanning characters

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    197

    scanning characters

    hi ...I am in a situation to scan a string (character by character).....i was doing it with cin

    but i was asked to do it using "cstdio"....
    Code:
    char c[maxedges][maxedges];
    
    for(i=0;i<n;i++)
    {
    
    for(j=0;j<m;j++)
    {
    
    cin>>c[i][j];
    }
    }
    Code:
    scanf("%c",&c[i][j])  dint work as expected
    plz help me to scan....wat shud i do


    must be a silly question though

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    %c will take carriage-return (for example) as a perfectly good character. What are you getting that you don't want?

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    check out this simple code....
    t--testcase
    n--no.of rows
    m --no,of columns

    Code:
    #include<iostream>
    #include<stdio.h>
    #include<queue>
    #include<vector>
    #include<cstdio>
    #define max 999999
    #define maxedges 1009
    #include<set>
    using namespace std;
    int main()
    {
    long long int t;
    cin>>t;
    
      char c[maxedges][maxedges];
      static long long int i,j,n,m;
      static long long int count=0; 
     while(t)
     {
    
      count=0;
      cin>>m;
      cin>>n;
    for(i=0;i<n;i++)
    {
    for(j=0;j<m;j++)
    {
    
    //scanf("%c",&c[i][j]);
    cin>>c[i][j];
    if((c[i][j]=='.'))
    {
    count++;
    }
    
    
    }}
    
    count=0;
    
    for(i=0;i<n;i++)
    {
    
    for(j=0;j<m;j++)
    {
                    
                    cout<<c[i][j];
    }
    cout<<"\n";
    }
    t--;
    }
    return 0;
    }

    INPUT
    1
    7 6
    #######
    #.#.###
    #.#.###
    #.#.#.#
    #.....#
    #######
    EXPECTED OUTPUT IS GOT USING CIN:
    Code:
    #######
    #.#.###
    #.#.###
    #.#.#.#
    #.....#
    #######
    but not got using scanf

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    60
    various way to clean the stream junk of carriage return.

    1. use scanf() w/ discarding of char
    Code:
    scanf("%c%*c", &mychar);
    2. clean stream:
    Code:
    scanf("%c", &mychar);
    fflush(stdin);
    3. capture \r
    Code:
    scanf("%c", &mychar);
    getch(); // not working under *nix
    4. others: getche(); fgetc(); getc() ...

    use them smartly.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This resuls in undefined behaviour:
    Code:
    fflush(stdin);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    huh ;(
    thanks anyways

    but can u tell me wat exactly works for me....
    can i not use anything else other than scanf....

    can someone modify the scanning part of my example plz so that it works

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    thanks for the reply laserlight...i need to try it

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by dpp View Post
    huh ;(
    thanks anyways

    but can u tell me wat exactly works for me....
    can i not use anything else other than scanf....

    can someone modify the scanning part of my example plz so that it works
    You still have to tell us what's wrong with it. If the trouble is that you are reading \n when you don't want to, then use " %c" instead of "%c" and you won't pick up spaces or newlines.

  9. #9
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    laserlight but where should i add it in my code...(Silly qstn right) anyways plz tel me

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I did not tell you to add anything

    Have you read tabstop's posts?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    Tabstop is right. Just use
    Code:
                scanf(" %c",&c[i][j]);
    instead. It will correctly ignore newline.

  12. #12
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    yesssssss Tabstop was extremely helpfull .......thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  3. How do you check how many characters a user has entered?
    By engstudent363 in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 06:05 AM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  5. Scanning characters & strings
    By rain_e in forum C Programming
    Replies: 2
    Last Post: 03-04-2002, 01:43 AM