finding a space

This is a discussion on finding a space within the C++ Programming forums, part of the General Programming Boards category; hi, i'm a little confused. i'm trying to find a space in a string, but i can't. this works fine. ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    9

    finding a space

    hi, i'm a little confused. i'm trying to find a space in a string, but i can't.

    this works fine.
    Code:
    for(j = 0; j < 5; j++){
             cout<<row[i][j];
        
             if(row[i][j] == 'A'){
                    cout<<"found A";
                    break;
            }
    }
    but if i use space ( ' ' ) instead of 'A', if block is never evaluated as true.

    any thoughts?

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Is there a space in the string?

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    9
    ok guys,

    i guess i should've given you more info. i have an input file, and the contents are:
    Code:
    TRGSJ
    XDOKI
    M VLN
    WPAB
    UIHAB
    space is between M and V!!!!

    in my code i read the input:
    Code:
    for(i = 0; i < 6; i++)
           cin>>row[i];
    row is declared as: string row[i];

    when i search the row[i][j], where j is the character in the string, i can't find the space, but any other letter is ok (as i posted earlier).

    any suggestions?

  4. #4
    not-a-geek
    Join Date
    Apr 2004
    Posts
    210
    The code in your initial post will only parse the first line (or whichever value i currently has). You will need another loop for "i" to parse all lines.

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    9
    ya i know, but that's not the problem....i have the outter loop.

    ok the bottom line is:

    row[i][j] == ' ' will never evaluate to true, even if the character at the position i,j is space (' '). row[i][j] == 'A' however will evaluate to true if the character is A. i know all my other code is correct. i tried: '' and ' ' and "" and " "

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,265
    row[i][j] == ' ' is the correct way to evalue if there is a space there. There is an error in your code somewhere. Post both the inner and outer loops, and maybe someone here could spot the error for you.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    > cin>>row[i];
    cin does not read spaces (it's like scanf("%s") from C)

    Use cin.getline() if you want to read a whole line, spaces included
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    >> cin>>row[i];
    if you read the input that way, you'll get M and VLN in two different strings, won't you?
    You might wanna use getline() so that you'll read by line/row instead of by word
    [edit] Salem beat me to the punch [/edit]
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  9. #9
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    I can't see all of your code, but I believe your problem is because you are reading in the file one string at a time. The problem is, C++ reads in a string until it hits a whitespace character then stops so in your example above, row[0]="TRGSJ", row[1]="XDOKI", row[2]="M" and row[4]="VLN" etc.

    [edit] alpha and salem beat me to the punch [/edit]

  10. #10
    Registered User
    Join Date
    Jul 2004
    Posts
    9
    haha, ya, i just figured that out about cin. thanks a lot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Out of space when compiling kernel
    By NuNn in forum Linux Programming
    Replies: 3
    Last Post: 04-01-2009, 03:43 PM
  2. disk space mysteriously gone
    By evader in forum C++ Programming
    Replies: 4
    Last Post: 01-21-2004, 01:30 PM
  3. Replies: 12
    Last Post: 05-17-2003, 06:58 AM
  4. someone who is good at finding and fixing bugs?
    By elfjuice in forum C++ Programming
    Replies: 8
    Last Post: 06-07-2002, 04:59 PM

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