Thread: help understanding strings in c++

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    67

    help understanding strings in c++

    ok so I just stiched from c to c++ and im having problems using strings for some of my functions. this is my code
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main() {
      string item;
    
      while (cin >> item) {
        cout << item << endl;
        }
    
      return 0;
    }
    say i enter this info to it (in one line then press enter):
    1 2 3 4 5 + - display 12 *

    I get this

    Code:
    1 2 3 4 5 + - display 12 * delete
    1
    2
    3
    4
    5
    +
    -
    display
    12
    *
    delete
    now this is the part where im not understanding how to put it in code. Our teacher wants us do an add numbers to a stack and add or subtract some numbers is when enter the signs (+ -). I cant think of a way to do it. I just need help thinking not asking for you guys to do my homework just some enlightenment.

    thank you for reading.

    for like the one that is enter here how would I do this function to add a one(1) to the stack? how can it differentiate from letters and numbers?
    can I do some thing like this?

    Code:
    while (cin >> item) {
       
     if (item == int){
      add number one to stack;
     }
    
     if (item== "delete" ignoring capitalization)
      delete all the items from stack
     }
    
    
     }
    
    }
    Last edited by newbc; 06-29-2011 at 06:04 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I'm not sure that this is a string question, but a stack question. Do you know how to put anything on a stack? Do you know how to take things off a stack? Are you supposed to use the stack that's in C++ or write your own?

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    67
    Quote Originally Posted by tabstop View Post
    I'm not sure that this is a string question, but a stack question. Do you know how to put anything on a stack? Do you know how to take things off a stack? Are you supposed to use the stack that's in C++ or write your own?
    yes i do know how to put items in stack. what im having problems is taking the input like 1 2 3 in the same line and adding them to a stack using a function. Im used to doing it one in put per line not multiple like this. because later my program needs to delete all the items in a stack if the user types delete.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So put your stack input in your loop (perhaps in place of printing it to the screen) and you're done.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    67
    so i'm reading online and find a way to compare strings.

    if (user1.compare(user) == 0)

    just some questions is this case sensitive? or is Delete not the same as delete? any help on figuring out how to ignore capital letter?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by newbc
    if (user1.compare(user) == 0)

    just some questions is this case sensitive? or is Delete not the same as delete?
    Yes, case sensitive. You could have checked it youself. Incidentally, it is more common to write:
    Code:
    if (user1 == user)
    Quote Originally Posted by newbc
    any help on figuring out how to ignore capital letter?
    Convert both strings to uppercase or lowercase.
    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

  7. #7
    Registered User
    Join Date
    Nov 2010
    Posts
    67
    thanks for the help laserlight


    lets say I want to check if the character is an int how would do that i found a function online to convert a string to int
    , but im having a problem checking the string value using an if statement.

    since I'm asking them to enter characters how would do an if statement to check it?

    Code:
    string item;
    
    if (item = a number){
     do this
    }

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by newbc
    lets say I want to check if the character is an int how would do that i found a function online to convert a string to int
    We call the above a run-on sentence. Calm down and elaborate on what exactly it is you want to check. It may help to provide an example.
    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

  9. #9
    Registered User
    Join Date
    Nov 2010
    Posts
    67
    Quote Originally Posted by laserlight View Post
    We call the above a run-on sentence. Calm down and elaborate on what exactly it is you want to check. It may help to provide an example.
    lets say I'm using the first example I posted on top. Since it saves everything as characters, how can I know if the character is actually a number? using an if statement.
    The user then enters this data: 1 car 2
    so now it needs to check if 1 and 2 are actually ints.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main() {
      string item;
    
      while (cin >> item) {
        
       if (item = a number){
         do this
       }
    
        }
    
      return 0;
    }

    Basically how do i check if the character is an int?

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That is what isdigit is for. There's also isalpha, isupper, islower, and lots of things that live in <cctype>.

  11. #11
    Registered User
    Join Date
    Nov 2010
    Posts
    67
    Quote Originally Posted by tabstop View Post
    That is what isdigit is for. There's also isalpha, isupper, islower, and lots of things that live in <cctype>.
    thank u I knew I read it somewhere but I could not find it online. thanks. I guess this works for both c and c++ right?

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It does, but they want a C-style string. So if you have a std::string, call the c_str() member function to get a C-style string to pass to those functions.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    It might be easier to do this:
    Code:
    double number 
    if(cin >> number){
      //do number stuff
    } else {
      cin.clear(ios::failbit);
      if(cin >> str){
        //do operator stuff
      }
    }
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Swapping strings in an array of strings
    By dannyzimbabwe in forum C Programming
    Replies: 3
    Last Post: 03-03-2009, 12:28 PM
  2. help understanding arrays of strings!
    By smoking81 in forum C Programming
    Replies: 18
    Last Post: 02-23-2008, 04:24 AM
  3. malloc() strings VS array strings
    By Kleid-0 in forum C Programming
    Replies: 5
    Last Post: 01-10-2005, 10:26 PM
  4. Table mapping Strings to Strings
    By johnmcg in forum C Programming
    Replies: 4
    Last Post: 09-05-2003, 11:04 AM
  5. converting c style strings to c++ strings
    By fbplayr78 in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2003, 03:13 AM