while loop

This is a discussion on while loop within the C++ Programming forums, part of the General Programming Boards category; I'm trying to get my first while loop to quit on blank spaces.... I have tried for awhile but am ...

  1. #1
    Registered User lord's Avatar
    Join Date
    Dec 2006
    Posts
    61

    while loop

    I'm trying to get my first while loop to quit on blank spaces.... I have tried for awhile but am officially stuck.

    CODE: http://pastebin.com/d21f81f7c

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    while(counter < 20 && dataIn != '/b')
    guess this thingy '/b' is called a multibyte char literal.
    it's not what you want
    try
    Code:
    while(counter < 20 && dataIn != '\b')
    Kurt

  3. #3
    Registered User lord's Avatar
    Join Date
    Dec 2006
    Posts
    61
    opps! ... but I have tried a lot of different ways (including \b) still no luck...

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    And a blank space is ' ', not '\b' (which is backspace, maybe?)

  5. #5
    Registered User lord's Avatar
    Join Date
    Dec 2006
    Posts
    61
    it's telling me that != doesn't take type char... but I have to use char

  6. #6
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by lord View Post
    it's telling me that != doesn't take type char... but I have to use char
    No it is telling you that you cannot compare a filestream to a char.
    Kurt

  7. #7
    Registered User lord's Avatar
    Join Date
    Dec 2006
    Posts
    61
    Kurt, that helped me get on a new track... thanks.

    But why does this not do it then?

    while(counter < 20 && (song[n].title[counter] != ' '))
    dataIn.get(song[n].title[counter++]);

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by lord View Post
    Kurt, that helped me get on a new track... thanks.

    But why does this not do it then?

    while(counter < 20 && (song[n].title[counter] != ' '))
    dataIn.get(song[n].title[counter++]);
    Because you check whether something is a space before you read it in?

  9. #9
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Guess it does what it should do. But you didn't terminate the string.
    try
    Code:
    while(counter < 20 && (song[n].title[counter] != ' '))
        dataIn.get(song[n].title[counter++]);
    song[n].title[counter] = 0;
    Kurt

  10. #10
    Registered User lord's Avatar
    Join Date
    Dec 2006
    Posts
    61
    none of that worked.... even if I put:

    while(counter < 20 && (song[n].title[12] != ' '))
    dataIn.get(song[n].title[counter++]);

    where I know there is a blank it does nothing... ?

  11. #11
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by lord View Post
    ... it does nothing... ?
    What do you expect it to do ?
    Kurt

  12. #12
    Registered User lord's Avatar
    Join Date
    Dec 2006
    Posts
    61
    I just realized in the file (song.txt) things are separated not by spaces but by tabs...

  13. #13
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You could use getline, and use the tab character ('\t') as a delimiter.

    Another thing is using char arrays. Are you sure that none of the items in the file is longer than 19 characters? In any case, the code would be much more robust and easier to get right, if you used the C++ standard string class.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 11:21 PM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 03:14 AM
  3. loop in a linked linked lists
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 08-26-2006, 01:38 PM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-20-2005, 12:30 AM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM

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