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;
}