is there a better way? new to c++ - c++

I am new to c++ but do have a basic knowledge in coding. This program works fine and well but I'm wondering if there is a better way to do this.
The program makes a star wars name by taking the first three letters of your last name and the first 2 of your first name to make your first name of your star wars name. Then for your star wars surname it takes the first two letters of your mother's maiden name and the first three letters of the city you were born in.
// starWarsName.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
string firstName;
string surname;
string maidenName;
string city;
cout << "This program is designed to make you a star wars name, it takes some information and concatinates parts of the information to make your NEW name" <<endl << endl;
cout << "please enter your first name" << endl;
cin >> firstName;
cout << "please enter your surname" <<endl;
cin >> surname;
cout << "what is your mothers maiden name?" << endl;
cin >> maidenName;
cout << "please tel me which city you were born in" << endl;
cin >> city;
cout << firstName << " " << surname << endl;
cout << firstName[0] << " " << surname << endl;
int size = firstName.length();
//cout << size;
cout << surname[0] << surname[1] << surname[2] << firstName[0] << firstName[1];
cout << " " << maidenName[0] << maidenName[1] << city[0] << city[1] << city[2];
cin.get();
cin.ignore();
return 0;
}

You can use string::substr here to store character sequence instead of writing surname[0]..surname[2] again and again.
Here is an example of string::substr
#include <iostream>
#include <string>
int main ()
{
std::string str="We think in generalities, but we live in details.";
// (quoting Alfred N. Whitehead)
std::string str2 = str.substr (3,5); // "think"
std::size_t pos = str.find("live"); // position of "live" in str
std::string str3 = str.substr (pos); // get from "live" to the end
std::cout << str2 << ' ' << str3 << '\n';
return 0;
}
Output:
think live in details.

Related

I am having an issue with the getline function [duplicate]

This question already has answers here:
cin and getline skipping input [duplicate]
(4 answers)
Why does std::getline() skip input after a formatted extraction?
(5 answers)
Closed 21 days ago.
#include <iostream>
using namespace std;
int main(){
//set variables
string name1;string name2;string name3;string n1;string n2;string n3;
int age1; int age2; int age3;
//get names and ages
cout << "Who Is The First Student?"<< endl;
getline(cin, name1); //name 1
cout << "What is " << name1<< "'s Age?"<< endl;
cin >> age1; // age 1
cout << "Who Is The Second Student?"<< endl;
getline(cin, name2); //name 2
cout << "What is " << name2<< "'s Age?"<< endl;
cin >> age2; //age 2
cout << "Who Is The Third Student?"<< endl;
getline(cin, name3); // name 3
cout << "What is " << name3<< "'s Age?"<< endl;
cin >> age3; //age 3
// gets modified names
n1 = name1.substr(2, name1.size() -3);
n2 = name2.substr(2, name2.size() -3);
n3 = name3.substr(2, name3.size()-3);
// Output formatting
cout << "Name Age Modified"<<endl;
cout << name1<< " "<<age1<<" "<<n1<<endl;
cout << name2<< " "<<age2<<" "<<n2<<endl;
cout << name3<< " "<<age3<<" "<<n3<<endl;
return 0;
}
The output asks the first question which is for the name of the first student but it outputs as this:
Who Is The First Student?-
John Doe-
What is John's Age?-
19-
Who Is The Second Student?-
What is 's Age?-
It is skipping the user input of the second student's name and instantly asking for the age but I don't know why this is happening, is there something wrong with my code or do I have the formatting incorrect? I believe that I used the getline function correctly but I may be incorrect and unaware of it being skipped over by a more important function.
The std::string::substr() man page says this about the exception you are seeing:
Parameters
pos - position of the first character to include
count - length of the substring
Exceptions
[throws] std::out_of_range if pos > size()
The main problem of the program is that the operator >> reads only one word.
That is for example in these statements
cout << "Who Is The First Student?"<< endl;
cin >> name1; //name 1
You entered a string that contains two words "John Doe". But the operator >> reads only the first word "Jphn" in the variable name1.
So in the next input statement
cout << "What is " << name1<< "'s Age?"<< endl;
cin >> age1; // age 1
an error occurred because instead of a number the input buffer contains the word "Doe".
So as a result the variables name1, name2, and name3 do not contain what you are expecting. And these statements
n1 = name1.substr(2, name1.size()-3);
n2 = name2.substr(2, name2.size()-3);
n3 = name3.substr(2, name3.size()-3);
produce the run-time error.
Instead of the operator >> you should use standard function std::getline.
Here is a demonstration program based on the code of your program that shows what is the reason of the error
#include <iostream>
#include <string>
int main()
{
std::string name1, name2;
int age1;
//get names and ages
std::cout << "Input 3 Names Below:" << std::endl;
std::cout << "Who Is The First Student?" << std::endl;
std::cin >> name1; //name 1
std::cout << "What is " << name1 << "'s Age?" << std::endl;
std::cin >> age1; // age 1
std::cout << "Who Is The Second Student?" << std::endl;
std::cin >> name2; //name 2
std::cout << "name1 = " << name1 << '\n';
std::cout << "name2 = " << name2 << '\n';
}
The program output is
Input 3 Names Below:
Who Is The First Student?
John Doe
What is John's Age?
Who Is The Second Student?
name1 = John
name2 =
As you can see due to the error of reading a data in the variable age1 (the buffer contains the string "Doe") the object name2 is empty. So if you will use it in this statement
n2 = name2.substr(2, name2.size()-3);
then the runtime error will occur.
Here is another demonstration program that shows how the standard function std::getline can be used in your program.
#include <iostream>
#include <string>
#include <limits>
int main()
{
std::string name1, name2, name3;
int age1, age2, age3;
//get names and ages
std::cout << "Input 3 Names Below:" << std::endl;
std::cout << "Who Is The First Student?" << std::endl;
std::getline( std::cin, name1 ); //name 1
std::cout << "What is " << name1 << "'s Age?" << std::endl;
std::cin >> age1; // age 1
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
std::cout << "Who Is The Second Student?" << std::endl;
std::getline( std::cin, name2 ); //name 2
std::cout << "What is " << name2 << "'s Age?" << std::endl;
std::cin >> age2; //age 2
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
std::cout << "Who Is The Third Student?" << std::endl;
std::getline( std::cin, name3 ); // name 3
std::cout << "What is " << name3 << "'s Age?" << std::endl;
std::cin >> age3;
std::cout << "name1 = " << name1 << '\n';
std::cout << "name2 = " << name2 << '\n';
std::cout << "name3 = " << name3 << '\n';
}
The program output is
Input 3 Names Below:
Who Is The First Student?
John Dow
What is John Dow's Age?
20
Who Is The Second Student?
Mary Poppins
What is Mary Poppins's Age?
21
Who Is The Third Student?
Bob Fisher
What is Bob Fisher's Age?
25
name1 = John Dow
name2 = Mary Poppins
name3 = Bob Fisher

How do I add vectors into my structs to create an inventory system in which I can add multiple different wines to the system using only one struct?

I have a school assignment in which I have to create a Wine Inventory System where the user can add multiple different wines without a finite number I assume.
I need to create vectors but I'm not sure. I don't know what to try.
#include <string>
#include <iostream>
#include <vector>
using namespace std;
struct Wine1
{ //struct for Wine pssibly needs Vector
string name;
string year;
string place;
string price;
} wine;
void printwine(Wine1 wine);
int main()
{
string str; //input for data
cout << "Please enter the data of the First wine: " << endl;
cout << "Enter name: ";
getline(cin, wine.name);
cout << endl << "Enter year: ";
getline(cin, wine.year);
cout << endl << "enter country of creation: ";
getline(cin, wine.place);
cout << endl << "enter price: ";
getline(cin, wine.price);
cout << endl;
cout << "your entered data: " << endl;
printwine(wine);
cout << endl;
printwine2(wine2);
cout << endl;
printwine3(wine3);
}
void printwine(Wine1 wine)
{ //data the user typed as output
cout << "Wine1" << endl;
cout << "the name is: " << wine.name << endl;
cout << "it's year is: " << wine.year << endl;;
cout << "its country of creation is: " << wine.place << endl;;
cout << "it's price is: " << wine.price << endl;
}
It should output the name of the wine, the year, it's country, and it's price, for each wine which was added.
A good starting should be using vector of Wine1.
std::vector<Wine1> wineVec;
wineVec.reserve(/*size*/) // reserve the memory if you know the number of wines beforehand
The printwine function should now take std::vector<Wine1>(preferably const-reference as the data is read-only) and iterate through the vector to print the attributes of the Wine1.
Something like:
#include <string>
#include <iostream>
#include <vector>
void printwine(const std::vector<Wine1>& vecWine)
{
for (const auto& wine : vecWine)
{
// do printing each: wine.name, wine.year,... so on
}
}
int main()
{
std::vector<Wine1> vecWine;
int wineNumber = 2;
vecWine.reserve(wineNumber);
std::string name, year, place, price;
for (int i = 0; i < wineNumber; ++i)
{
// get the user input for name, year, place, and price
std::cin >> name >> year >> place >> price;
vecWine.emplace_back(Wine1{ name, year, place, price });
}
printwine(vecWine);
}
That said, you should read more about std::vector
to get to know more, how it works.
Also, good to read about, how to overload operator>> and operator<<, so that you could even write code, much simpler.
Following is an incomplete code, which I leave you to complete after covering the topics which I mentioned.
void printwine(const std::vector<Wine1>& vecWine)
{
for (const auto& wine : vecWine)
{
std::cout << wine << '\n';
}
}
int main()
{
std::vector<Wine1> vecWine(wineNumber);
for (Wine1& wine : vecWine)
{
std::cin >> wine;
}
printwine(vecWine);
}
I believe there is a misunderstanding: you do not intend your struct Wine1 to contain a vector but instead, you want a vector of Wine1's.
I suggest a data structure similar to the following:
struct Wine {
string name;
string year;
string place;
string price;
};
void printwinelist(vector<Wine>& list){
for(Wine& w : list){
printwine(w);
}
}
vector<Wine> winelist;
The main method has to be rewritten accordingly, to append additional objects to the vector.
While I could rewrite your code accordingly I suspect, that a better next step for you would be to read up on some of the concepts used, such as vectors.
You probably want something like this:
#include <string>
#include <iostream>
#include <vector>
using namespace std;
struct Wine1
{ //struct for Wine pssibly needs Vector
string name;
string year;
string place;
string price;
};
void printwine(Wine1 wine);
int main()
{
vector<Wine1> wineinventory;
// read 3 wines
for (int i = 3; i < 10; i++)
{
Wine1 wine;
string str; //input for data
cout << "Please enter the data of the First wine: " << endl;
cout << "Enter name: ";
getline(cin, wine.name);
cout << endl << "Enter year: ";
getline(cin, wine.year);
cout << endl << "enter country of creation: ";
getline(cin, wine.place);
cout << endl << "enter price: ";
getline(cin, wine.price);
cout << endl;
cout << "your entered data: " << endl;
printwine(wine);
wineinventory.push_back(wine); // store in vectore
}
// print all wines in the vector
for (int i = 0; i < wineinventory.size(); i++)
{
cout << "Wine " << i << ":" endl;
printwine(wineinventory[i]);
}
}
Disclaimer: this is untested code, I'm not even sure if it compiles, but you should get the idea.
There is still much room for improvement.

Getline not recognized by compiler

I have just started C++ after working with C for almost a year. I'm writing a program for a user to input info about a song. I read that I should use getline() to read strings with spaces. Here is my code:
#include <string>
#include <cstring>
#include <iostream>
using namespace std;
int main()
{
typedef struct Song
{
char title[20];
char album[20];
char artist[20];
int year;
} song;
//store song info
Song Input;
char inputStr[20];
int inputYear;
cout << "Enter the name of a song: ";
getline(cin, inputStr);
strcpy(Input.title, inputStr);
cout << "Enter the album of your song: ";
getline(cin, inputStr);
strcpy(Input.album, inputStr);
cout << "Enter the artist of your song: ";
getline(cin, inputStr);
strcpy(Input.artist, inputStr);
cout << "Enter the year your song was released: ";
cin >> inputYear;
Input.year = inputYear;
//print
cout << "Song title: " << Input.title << endl;
cout << "From album: " << Input.album << endl;
cout << "Artist: " << Input.artist << endl;
cout << "Released: " << Input.year << endl;
return 0;
}
My compiler1 throws 3 errors, one for each of the getline() calls, not recognizing getline() despite the fact I have #include <string>. I have looked up sample usage of the getline() function.
Thanks for any help.
1I have wondered if this problem might concern an issue with the standard of C++ that my compiler supports. I did a bit of research and I did not find anything that helped me learn which standard I am using. Here's some info:
I'm using Terminal on Mac.
After g++ version:
Configured with: --prefix=/Applications/Xcode.app/Cont.../usr/include/c++/4.2.1
Apple LLVM version 8.1.0 (clang-802.0.42)
These lines seem to be the only ones of use here, but I could give more info. If someone has any idea which standard of C++ this is, whether it's C++11, or C++14, or otherwise, that would also be very helpful. Thanks again.
UPDATE:
started from scratch, tried to take as much of your advice as possible while still sticking to some of what I know. No errors and works just as I hoped. Thanks for all your help.
New code:
#include <string>
#include <cstring>
#include <iostream>
using namespace std;
struct Song
{
string title;
string artist;
string album;
string year;
}song;
int main()
{
Song Input;
cout << "Song? ";
getline(cin, Input.title);
cout << "Artist? ";
getline(cin, Input.artist);
cout << "Album? ";
getline(cin, Input.album);
cout << "Year? ";
getline(cin, Input.year);
cout << "Song: " << Input.title << endl;
cout << "Artist: " << Input.artist << endl;
cout << "Album: " << Input.album << endl;
cout << "Year: " << Input.year << endl;
return 0;
}
The version of getline you are using takes a std::string as a parameter, not an array of char. If you want to use an array of char (and you shouldn't), you need to use the member function version:
cin.getline( some_char_array, array_size );
I would switch from using char arrays to using string's everywhere. For example I would do your code like this:
#include <string>
#include <iostream>
struct Song{
std::string title;
std::string album;
std::string artist;
int year;
};
int main()
{
//store song info
Song Input;
int inputYear;
std::cout << "Enter the name of a song: ";
getline(std::cin, Input.title);
std::cout << "Enter the album of your song: ";
getline(std::cin, Input.album);
std::cout << "Enter the artist of your song: ";
getline(std::cin, Input.artist);
std::cout << "Enter the year your song was released: ";
std::cin >> Input.year;
//print
std::cout << "Song title: " << Input.title << '\n';
std::cout << "From album: " << Input.album << '\n';
std::cout << "Artist: " << Input.artist << '\n';
std::cout << "Released: " << Input.year << std::endl;
return 0;
}
My preference is to not use using namespace std; but there's nothing wrong with it. Notice that using strings directly I don't need to copy things. I can use getline to do all that for me. I also don't need to worry about overrunning the size of the char array because string does that for me as well.

How do I take a users input that has spaces and return it to a string?

/* Programmer: Joshua Zuber
Program Desc: This is a game that can keep track of players scores!
Program Name: Score Tracker
Clean Compile Date:
*/
#include<iostream>
#include<string>
#include<iomanip>
#include<cstdlib>
#include<ctime>
#include <vector>
using namespace std;
int main()
{
// Player Variables
string player1
string player2;
string player3;
string player4;
string player5;
// Variables
char again = 'y';
char menu1 = 'y';
int pNumb;
// Game Arrays
string player1G[] = "";
string player2G[] = "";
string player3G[] = "";
string player4G[] = "";
string player5G[] = "";
// Score Arrays
string player1S[] = "";
string player2S[] = "";
string player3S[] = "";
string player4S[] = "";
string player5S[] = "";
while (toupper(again) == 'Y')
{
cout << "Welcome to Score Tracker!" << "\n\n";
while (toupper(menu1) == 'Y')
{
cout << "1. " << player1 << "\n\n";
cout << "2. " << player2 << "\n\n";
cout << "3. " << player3 << "\n\n";
cout << "4. " << player4 << "\n\n";
cout << "5. " << player5 << "\n\n";
cout << "Please select a player to add your favorite games and scores!";
cin >> pNumb;
system("cls");
if (pNumb == 1)
{
cout << "Please enter a name for player 1!" << "\n\n";
cin >> player1;
cin.getline(player1);
cout << "\n\n" << "Would you like to return to menu? Y/N " << "\n\n";
cin >> menu1 ;
system("cls");
}
}
}
}
How do I take a users input that has spaces and return it to a string?
I am trying to make a simple program that takes 5 users names and has them input their 3 favorite games but i am having trouble with just getting them to input their names for the menu.
The reason your behavior is happening is because of these 2 lines:
cin >> player1;
cin.getline(player1);
In the first line, cin will ignore whitespace, and in the second one, it will not. So just remove the first line in the snippet above, and it should accept a name with spaces.

String phrase ends at space?

I am wringing a simple code to learn more about string. When I ran my code it would not print my last name. Can someone explain why? I used string phrase to store it and it only appears to have stored my first name. Here is the code.
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main()
{
cout << "Exercise 3B" << endl;
cout << "Kaitlin Stevers" << endl;
cout << "String arrays" << endl;
cout << endl;
cout << endl;
char greeting[26];
cout << "Please enter a greeting: " << endl;
cin >> greeting;
cout << "The greeting you entered was: " << greeting << endl;
string phrase;
cout << "Enter your full name " << endl;
cin >> phrase;
cout << greeting << ", how are you today " << phrase << "?" << endl;
return 0;
}
I used string phrase to store it and it only appears to have stored my first name.
That makes sense.
cin >> phrase;
will stop reading when it encounters a whitespace character in the input.
To read the full name you can use one of the following approaches.
Use two calls to cin >>.
std::string first_name;
std::string last_name;
cin >> first_name >> last_name;
Use getline to read the entire line. getline will read everything in a a line, including whitespace characters.
getline(cin, phrase);
When you call cin >> phrase;, it only reads the string up to the first non-space character. If you want to include spaces in your name, best goes with getline(cin,phrase);.
IMPORTANT: getline() will reads whatever it is in the stream buffer up to the first \n. It means that when you enter cin >> greeting;, if you hit ENTER, getline() will read everything before that \n that is not already read, which is NOTHING into your phrase variable, making it an empty string. An easy way out is to call getline() twice. E.g.
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main()
{
cout << "Exercise 3B" << endl;
cout << "Kaitlin Stevers" << endl;
cout << "String arrays" << endl;
cout << endl;
cout << endl;
char greeting[26];
cout << "Please enter a greeting: " << endl;
cin >> greeting; //IMPORTANT: THIS ASSUME THAT GREETING IS A SINGLE WORD (NO SPACES)
cout << "The greeting you entered was: " << greeting << endl;
string phrase;
cout << "Enter your full name " << endl;
string rubbish_to_be_ignored;
getline(cin,rubbish_to_be_ignored); //this is going to read nothing
getline(cin, phrase); // read the actual name (first name and all)
cout << greeting << ", how are you today " << phrase << "?" << endl;
return 0;
}
Assuming you store that code in the file stackoverflow.cpp. Sample run:
Chip Chip#04:26:00:~ >>> g++ stackoverflow.cpp -o a.out
Chip Chip#04:26:33:~ >>> ./a.out
Exercise 3B
Kaitlin Stevers
String arrays
Please enter a greeting:
Hello
The greeting you entered was: Hello
Enter your full name
Kaitlin Stevers
Hello, how are you today Kaitlin Stevers?
Tested on ubuntu 14.04