scanning characters

This is a discussion on scanning characters within the C++ Programming forums, part of the General Programming Boards category; hi ...I am in a situation to scan a string (character by character).....i was doing it with cin but i ...

  1. #1
    dpp
    dpp is offline
    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,185
    %c will take carriage-return (for example) as a perfectly good character. What are you getting that you don't want?

  3. #3
    dpp
    dpp is offline
    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
    20,975
    This resuls in undefined behaviour:
    Code:
    fflush(stdin);
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    dpp
    dpp is offline
    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
    dpp
    dpp is offline
    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,185
    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
    dpp
    dpp is offline
    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
    20,975
    I did not tell you to add anything

    Have you read tabstop's posts?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    dpp
    dpp is offline
    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, 12:43 AM

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