Thread: C++ strings

  1. #1
    Registered User immy's Avatar
    Join Date
    Apr 2014
    Posts
    33

    C++ strings

    Hello,

    My question is on c++ strings. At the moment my program is reading input in one line at a time after the user presses enter.

    I want to read multiple values in on a single line
    Example: "apple banana orange end"

    How would I do this?

    MAIN
    Code:
    #include "Header.h"
    #include "Orange.h"
    #include <iostream>
    #include <string>
    #include <cctype>
    using namespace std;
    
    int main()
    {
        string s;
        Orange type;
        cout << "<end> to exit program" << endl;
        do
        {
            cout << "\nEnter <banana>, <apple>, or <orange>: ";
    
                getline(cin, s);
            
    
            type.Input(s);
            type.Error_message(s);
            type.Fruit_count(s, 0, 0, 0);
            type.End(s);
        
        } while (s != "end");
    
        cout << "Total Bananas: " << type.total_banana(0) << endl;
        cout << "Total cost of Bananas: $" << type.total_cost_banana(1.20) << endl << endl;
    
        cout << "Total Apples: " << type.total_apple(0) << endl;
        cout << "Total cost of Apples: $" << type.total_cost_apple(0.50) << endl << endl;
    
        cout << "Total Oranges: " << type.total_orange(0) << endl;
        cout << "Total cost of Oranges: $" << type.total_cost_orange(0.60) << endl << endl;
    
        cout << endl;
    
        system("PAUSE"); 
        return 0;
    }
    CLASS HEADER

    Code:
    #pragma once
    #include <iostream>
    #include <string>
    using namespace std;
    class Orange
    {
    private:
        int total_banana_count = 0, total_apple_count = 0, total_orange_count = 0;
        string s;
        string x;
    public:
        void Input(string x);
        void Error_message(string s);
        void Fruit_count(string s, int banana_count, int apple_count, int orange_count );
        void End(string s);
        int total_banana(int z);
        double total_cost_banana(double z);
        int total_apple(int y);
        double total_cost_apple(double y);
        int total_orange(int h);
        double total_cost_orange(double h);
    };
    CLASS CPP

    Code:
    #include "Orange.h"
    #include <iostream>
    #include <string>
    using namespace std;
    
    void Orange::Input(string x)
    {
        s = x;
    }
    
     
    void Orange::Error_message(string s)
    {
        if (s != "banana" && s != "apple" && s != "orange" && s != "end")
        {
            cout << "Incorrect entry: " << endl;
             
        }
    }
    
    void Orange::Fruit_count(string s, int banana_count, int apple_count, int orange_count)
    {
    
        if (s == "banana")
        {
            total_banana_count++;
            total_banana_count += banana_count;
             
            //return banana_count;
         
        }
        else if (s == "apple")
        {
            total_apple_count++;
            total_apple_count += apple_count;
             
            //return apple_count;
     
        }
        else if (s == "orange")
        {
            orange_count++;
            total_orange_count += orange_count;
             
            //return orange_count;
    
        }
         
    }
    
    void Orange::End(string s)
    {
        while (s == "end")
        {
            cout << "\nProgram terminating" << endl;
            break;
        }
    
    }
    
    int Orange::total_banana(int z)
    {
        z = total_banana_count;
        return total_banana_count;
    }
    
    double Orange::total_cost_banana(double z)
    {
        z *= total_banana_count;
         
        return z;
    }
    
    int Orange::total_apple(int y)
    {
        y = total_apple_count;
        return total_apple_count;
    }
    
    double Orange::total_cost_apple(double y)
    {
         
        if (total_apple_count % 3 == 0)
        {
                int y = total_apple_count / 3;
                y * 0.5;
    
                return y;
        }
        else 
        {
            int z = total_apple_count / 3;
                y *= total_apple_count - z;
                return y;
         }
    }
    
    int Orange::total_orange(int h)
    {
        h = total_orange_count;
        return total_orange_count;
    }
    
    double Orange::total_cost_orange(double h)
    {
        h *= total_orange_count;
        return h;
    }
    Last edited by immy; 07-28-2014 at 06:38 AM.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    cin >> s;

  3. #3
    Registered User immy's Avatar
    Join Date
    Apr 2014
    Posts
    33
    You could of left a serious answer

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    It is a serious answer. cin >> s; does what you want.

  5. #5
    Registered User immy's Avatar
    Join Date
    Apr 2014
    Posts
    33
    cin >> s would store 1 variable (allowing one input). I want to store several variables on the same line: "apple banana orange end"

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You're handling the input one variable at a time anyway. It's just a matter of how you type the input, really. Look.
    Code:
    Josh2@Josh-PC /cygdrive/c/users/josh2/desktop
    $ ./foo
    apple orange banana end
    Received:
    "apple"
    "orange"
    "banana"
    "end"
    
    Josh2@Josh-PC /cygdrive/c/users/josh2/desktop
    $ cat foo.cpp
    #include <iostream>
    #include <string>
    #include <vector>
    using namespace std;
    
    int main()
    {
        vector<string> inputs;
        string s;
        do
        {
            cin>>s;
            inputs.push_back(s);
        } while (s != "end");
    
        cout<<"Received:"<<endl;
        for (vector<string>::iterator it = inputs.begin(); it != inputs.end(); it++)
        {
            cout<<'\"'<<*it<<'\"'<<endl;
        }
    }
    This example uses s to parse every word in turn and uses a vector to keep all of the inputs so that you can see that they were read, even though, like you say, cin >> s allows for one variable only. For your program, the vector is unnecessary.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by immy View Post
    cin >> s would store 1 variable (allowing one input). I want to store several variables on the same line: "apple banana orange end"
    Have you even bothered to read up on what stream operators do?

    "cin >> a >> b" will read values for two variables a and b. "cin >> s" done n times will also read something into s, n times.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    Registered User immy's Avatar
    Join Date
    Apr 2014
    Posts
    33
    Quote Originally Posted by whiteflags View Post
    You're handling the input one variable at a time anyway. It's just a matter of how you type the input, really. Look.
    Code:
    Josh2@Josh-PC /cygdrive/c/users/josh2/desktop
    $ ./foo
    apple orange banana end
    Received:
    "apple"
    "orange"
    "banana"
    "end"
    
    Josh2@Josh-PC /cygdrive/c/users/josh2/desktop
    $ cat foo.cpp
    #include <iostream>
    #include <string>
    #include <vector>
    using namespace std;
    
    int main()
    {
        vector<string> inputs;
        string s;
        do
        {
            cin>>s;
            inputs.push_back(s);
        } while (s != "end");
    
        cout<<"Received:"<<endl;
        for (vector<string>::iterator it = inputs.begin(); it != inputs.end(); it++)
        {
            cout<<'\"'<<*it<<'\"'<<endl;
        }
    }
    This example uses s to parse every word in turn and uses a vector to keep all of the inputs so that you can see that they were read, even though, like you say, cin >> s allows for one variable only. For your program, the vector is unnecessary.
    I havent yet covered "vectors" is there an alternative way of doing this with just using strings?

    Quote Originally Posted by grumpy View Post
    Have you even bothered to read up on what stream operators do?

    "cin >> a >> b" will read values for two variables a and b. "cin >> s" done n times will also read something into s, n times.
    I see, but how would I make it dynamic to my situation?

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by immy View Post
    I havent yet covered "vectors" is there an alternative way of doing this with just using strings?



    I see, but how would I make it dynamic to my situation?
    You could literally replace the getline() statement in your code with "cin >> s" and it will work. As I said before, you do not need vector at all. To be fair, you don't need vector in the example either:

    Code:
    Josh2@Josh-PC /cygdrive/c/users/josh2/desktop
    $ ./foo
    apple orange banana end
    "apple"
    "orange"
    "banana"
    "end"
    
    Josh2@Josh-PC /cygdrive/c/users/josh2/desktop
    $ cat foo.cpp
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
        string s;
        do
        {
            cin>>s;
            cout<<'\"'<<s<<'\"'<<endl;
        } while (s != "end");
    }
    It is merely a matter of how you type the input.

  10. #10
    Registered User immy's Avatar
    Join Date
    Apr 2014
    Posts
    33
    Quote Originally Posted by whiteflags View Post
    You could literally replace the getline() statement in your code with "cin >> s" and it will work. As I said before, you do not need vector at all. To be fair, you don't need vector in the example either:

    Code:
    Josh2@Josh-PC /cygdrive/c/users/josh2/desktop
    $ ./foo
    apple orange banana end
    "apple"
    "orange"
    "banana"
    "end"
    
    Josh2@Josh-PC /cygdrive/c/users/josh2/desktop
    $ cat foo.cpp
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
        string s;
        do
        {
            cin>>s;
            cout<<'\"'<<s<<'\"'<<endl;
        } while (s != "end");
    }
    It is merely a matter of how you type the input.



    It works, but I don't know why it is working....by simply doing cin >> s;

    Why is the cin working but not the getline?

    Could you explain in a simple way?

    Why are the two reacting so differently.
    Last edited by immy; 07-28-2014 at 12:13 PM.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by immy
    It works, but I don't know why it is working....by simply doing cin >> s;

    Why is the cin working but not the getline?

    Could you explain in a simple way?

    Why are the two reacting so differently.
    The idea of getline is to read line by line (or token by token, by changing the delimiter) into strings. Hence, your "multiple values in on a single line" is read as a single value.

    The idea behind cin >> s is formatted input, i.e., based on the type of the right hand side argument of operator>> and on other formatting instructions, input is read. By default, using cin >> s skips leading whitespace, reads the input into the string, stopping when whitespace is encountered. Hence, repeated use of operator>> for std::string reads each of your "multiple values in on a single line".
    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

  12. #12
    Registered User immy's Avatar
    Join Date
    Apr 2014
    Posts
    33
    Quote Originally Posted by laserlight View Post
    The idea of getline is to read line by line (or token by token, by changing the delimiter) into strings. Hence, your "multiple values in on a single line" is read as a single value.

    The idea behind cin >> s is formatted input, i.e., based on the type of the right hand side argument of operator>> and on other formatting instructions, input is read. By default, using cin >> s skips leading whitespace, reads the input into the string, stopping when whitespace is encountered. Hence, repeated use of operator>> for std::string reads each of your "multiple values in on a single line".
    Thank you for the helpful explanation, and thanks also to everyone who helped me with this problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-16-2012, 06:08 AM
  2. Swapping strings in an array of strings
    By dannyzimbabwe in forum C Programming
    Replies: 3
    Last Post: 03-03-2009, 12:28 PM
  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