help understanding strings in c++

This is a discussion on help understanding strings in c++ within the C++ Programming forums, part of the General Programming Boards category; ok so I just stiched from c to c++ and im having problems using strings for some of my functions. ...

  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,185
    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,185
    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
    21,313
    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.
    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

  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
    21,313
    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.
    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

  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,185
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    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,019
    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, 11:28 AM
  2. help understanding arrays of strings!
    By smoking81 in forum C Programming
    Replies: 18
    Last Post: 02-23-2008, 03:24 AM
  3. malloc() strings VS array strings
    By Kleid-0 in forum C Programming
    Replies: 5
    Last Post: 01-10-2005, 09: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

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