So, i'm curently reading Bjarne Stroustrup's "Programming : Principals and practice using c++" and i'm at chapter 3. There is a question in the book which is "Write a program that prompts the user to enter 3 string values, and then outputs the values in order separated by commas. So, if the user enters the values "Steinbeck", "Hemingway", "Fitzgerald", the ouput should be "Fitzgerald, Hemingway, Steinbeck"".
I am currently stuck at that questio开发者_Go百科n, I can't seem to make the programme work. I know how to output integers in numerical sequence, like with this code http://www.stroustrup.com/Programming/Solutions/Ch3/e3-6.cpp but I can't seem to modify that to make it work with string values (I get the error "fatal error LNK1120: 1 unresolved externals", and I wish to understand how to do so.
Any help is appreciated.
ps: Here is the modified code of what is in the link, (which is my lame attempt at answering the question). And I know there are simpler ways of doing so, but i'm not there yet, it's in the next chapter (vectors):
#include "D:\Mes Documents\Visual Studio 2010\std_lib_facilities.h"
int main()
{
string name1 = " ";
string name2 = " ";
string name3 = " ";
cout << "Please enter three names seperated by spaces: " << endl;
cin >> name1 >> name2 >> name3;
if (!cin) error("Something went wrong with the read");
string smallest = " ";
string middle = " ";
string biggest = " ";
if (name1<=name2 && name1<=name3)
{
smallest = name1;
if (name2<=name3)
{
middle = name2;
biggest = name3;
}
else
{
middle = name3;
biggest = name2;
}
}
else if (name2<=name1 && name2<=name3)
{
smallest = name2;
if (name1<=name3)
{
middle = name1;
biggest = name3;
}
else
{
middle = name3;
biggest = name1;
}
}
else
{
smallest = name3;
if (name1<=name2)
{
middle = name1;
biggest = name2;
}
else
{
middle = name2;
biggest =name1;
}
}
cout << "The names in order are: " << smallest << " " << middle << " " << biggest << endl;
system("pause");
return 0;
}
Solved #6 this way - a little bit different from what task is asking for. Will correct and post here a solution for 3.7 a little bit later.
#include "../../std_lib_facilities.h"
int main()
{
cout << "Enter three integers:\n";
int a = 0;
int b = 0;
int c = 0;
cin >> a >> b >> c;
if (a == b)
{
if (b == c)
{
cout << '\n' << a << " = " << b << " = " << c;
}
else if (a > c)
{
cout << '\n' << a << " = " << b << " > " << c;
}
else if (a < c)
{
cout << '\n' << c << " > " << a << " = " << b;
}
}
else if (a == c)
{
if (a > b)
{
cout << '\n' << a << " = " << c << " > " << b;
}
else if (a < b)
{
cout << '\n' << b << " > " << a << " = " << c;
}
}
else if (b == c)
{
if (b > a)
{
cout << '\n' << b << " = " << c << " > " << a;
}
else if (b < a)
{
cout << '\n' << a << " > " << b << " = " << c;
}
}
else if (a > b)
{
if (a > c)
{
if (b > c)
{
cout << '\n' << a << " > " << b << " > " << c;
}
else if (c > b)
{
cout << '\n' << a << " > " << c << " > " << b;
}
}
else if (a < c)
{
cout << '\n' << c << " > " << a << " > " << b;
}
}
else if (b > a)
{
if (b > c)
{
if (a > c)
{
cout << '\n' << b << " > " << a << " > " << c;
}
else if (c > a)
{
cout << '\n' << b << " > " << c << " > " << a;
}
}
else if (b < c)
{
cout << '\n' << c << " > " << b << " > " << a;
}
}
}
I've used VS 2013 Express for Windows Desktop. Settings as in B.3.1. Also i'm using the same project for all tasks. I just exclude old .cpp and create a new one. Make sure that you've excluded all your old cpp files! They seem to interfere with each other
So, "correct" solution. For 3.6 just change string to int and "Enter three names" to something else like "Enter three integers":
3.7
#include "../../std_lib_facilities.h"
int main()
{
cout << "Enter three names:\n";
string a = " ";
string b = " ";
string c = " ";
cin >> a >> b >> c;
if (a == b)
{
if (b == c)
{
cout << '\n' << a << ", " << b << ", " << c;
}
else if (a > c)
{
cout << '\n' << c << ", " << a << ", " << b;
}
else if (a < c)
{
cout << '\n' << a << ", " << b << ", " << c;
}
}
else if (a == c)
{
if (a > b)
{
cout << '\n' << b << ", " << a << ", " << c;
}
else if (a < b)
{
cout << '\n' << a << ", " << c << ", " << b;
}
}
else if (b == c)
{
if (b > a)
{
cout << '\n' << a << ", " << b << ", " << c;
}
else if (b < a)
{
cout << '\n' << b << ", " << c << ", " << a;
}
}
else if (a > b)
{
if (a > c)
{
if (b > c)
{
cout << '\n' << c << ", " << b << ", " << a;
}
else if (c > b)
{
cout << '\n' << b << ", " << c << ", " << a;
}
}
else if (a < c)
{
cout << '\n' << b << ", " << a << ", " << c;
}
}
else if (b > a)
{
if (b > c)
{
if (a > c)
{
cout << '\n' << c << ", " << a << ", " << b;
}
else if (c > a)
{
cout << '\n' << a << ", " << c << ", " << b;
}
}
else if (b < c)
{
cout << '\n' << a << ", " << b << ", " << c;
}
}
}
I've used the same link and modified to get required output: http://www.stroustrup.com/Programming/Solutions/Ch3/e3-6.cpp
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;
int main()
{
cout << "Please enter three string values separated by a space:\n";
string val1;
string val2;
string val3;
cin >> val1 >> val2 >> val3;
string smallest ="";
string middle ="";
string largest ="";
if (val1<=val2 && val1<=val3) {
smallest = val1;
if (val2<=val3) {
middle = val2;
largest = val3;
}
else {
middle = val3;
largest = val2;
}
}
else if (val2<=val1 && val2<=val3) {
smallest = val2;
if (val1<=val3) {
middle = val1;
largest = val3;
}
else {
middle = val3;
largest = val1;
}
}
else {
smallest = val3;
if (val1<=val2) {
middle = val1;
largest = val2;
}
else {
middle = val2;
largest = val1;
}
}
cout << "string values sorted : " << smallest << ", " << middle << ", " << largest <<'\n';
}
Output (Hope this helps):
Please enter three integer values separated by a space:
Steinbeck Hemingway Fitzgerald
values sorted : Fitzgerald, Hemingway, Steinbeck
精彩评论