//program8.cpp
#include <iostream>
#include "species.h"
#include "reptilian.h"
#include "mammalian.h"
#include "insects.h"
using namespace std;
void VirtualPrint(species &typeofSpecies){
typeofSpecies.printme();
}
void VirtualDanger(species &typeofSpecies){
typeofSpecies.showDanger();
}
int main(int argv, char **args){
reptilian rep;
insects ins;
VirtualPrint(rep);
VirtualPrint(ins);
VirtualDanger(rep);
VirtualDanger(ins);
return 1;
}
//species.h
#ifndef SPECIES_H
#define SPECIES_H
#include <iostream>
#include <string>
using namespace std;
class species{
public:
species();
virtual void printme();
virtual void showDanger() = 0;
protected:
string name;
string color;
int lifeExp;
bool thr;
};
#endif
//species.cpp
#include <iostream>
#include <string>
#include "species.h"
using namespace std;
species::species(){
cout << "Please enter name of species:" << endl;
getline(cin, name);
cout << "Please enter color of species:" << endl;
getline(cin, color);
cout << "Please enter life expectancy of species in years:" << endl;
cin >> lifeExp;
cout << "Please enter if species is threat:true(1) or false(0)" << endl;
cin >> thr;
}
void species::printme(){
cout << "Name: " << name << " Color: " << color << " Life Expectancy: " << lifeExp << " Threat: " << thr << endl;
}
//reptilian.h
#ifndef REPTILIAN_H
#define REPTILIAN_H
#include <iostream>
#include <string>
#include "species.h"
using namespace std;
class reptilian : public species{
public:
reptilian();
virtual void printme();
virtual void showDanger();
protected:
int length;
int lethality;
};
#endif
//reptilian.cpp
#include <iostream>
#include "reptilian.h"
using namespace std;
reptilian::reptilian(){
cout << "Please enter length(inches): " << endl;
cin >> length;
cout << "Please enter lethality(0-100): " << endl;
cin >> lethality;
}
void reptilian::printme(){
species::printme();
cout << " Length: " << length << " Lethality: " << lethality << endl;;
}
void reptilian::showDanger(){
cout << endl;
if(thr == true){
cout << "This species is a threat" << endl;
cout << "The name of the species is " << name << " has a color of " << color << ", has a life expectancy of " << lifeExp << " has a length of " << length << ", and a lethality of " << lethality << endl;
}
}
This is my code for my program it runs fine if one reptilian object is made. However when two are made it will not take the name of the second object. The same happens when two insects objects are made. I have not tested the mammalian object yet as I am trying to solve this problem first
edit:
output:
Please enter name of species:
sam
Please enter color of species:
orange
Please enter life expectancy of species in years:
100
Please enter if species is threat:true(1) or false(0)
0
Please enter length(inches):
60
Please enter lethality(0-100):
0
Please enter name of species:
Please enter color of species:
yellow
Please enter life expectancy of species in years:
20
Please enter if species is threat:true(1) or false(0)
0
Please enter length(inches):
10
Please enter lethality(0-100):
0
Please enter how venomous the species is(0-100):
0
Name: sam Color: orange Life Expectancy: 100 Threat: 0
Length: 60 Lethality: 0
Name: Color: yellow Life Expectancy: 20 Threat: 0
Length: 10 Lethality: 0
Venomous: 0
Your problem is mixing using getline with the >> operator. From http://en.cppreference.com/w/cpp/string/basic_string/getline:
When used immediately after whitespace-delimited input, e.g. after int n; std::cin >> n;, getline consumes the endline character left on the input stream by operator>>, and returns immediately. A common solution is to ignore all leftover characters on the line of input with cin.ignore(std::numeric_limits::max(), '\n'); before switching to line-oriented input.
So after the cin >> lethality a newline is left in the cin stream. The getline in the second instance of species sees this newline and immediately returns.
Also refer to this: std::cin:: and why a newline remains
To fix this, change your getline calls to use the cin >> method.
Related
#include <iostream>
#include <string>
#include <boost/algorithm/string.hpp>
using namespace std;
int main() {
string city1, city2;
cout << ("Please enter your citys name");
cin >> city1;
cout << ("Please enter your citys second name");
cin >> city2;
cout << city1 [0,1,2,3,4];
cout << city2 [0,1,2,3,4];
boost::to_upper(city1, city2);
cout << city1,city2;
}
This is my code and for some reason boost::to_upper(city1, city2); gets the error: [cquery] no matching funtion for call 'to_upper'
boost::algorithm::to_upper is declared as (from boost reference)
template<typename WritableRangeT>
void to_upper(WritableRangeT & Input, const std::locale & Loc = std::locale());
so you can pass only one string to this function. Replacing
boost::to_upper(city1, city2);
with
boost::to_upper(city1);
boost::to_upper(city2);
makes the code compile and an example output is Please enter your citys namePlease enter your citys second nameosLONDON.
It lacks newline characters and there is one more mistake - misunderstanding of comma operator. Generally comma is used to separate arguments or array elements but in lines
cout << city1 [0,1,2,3,4];
cout << city2 [0,1,2,3,4];
// ...
cout << city1,city2;
comma operator is used. Comma operators takes two operands and it's value is value of the right operand (e.g. after int x = (1, 2); variable x is equal to 2). Code above is equivalent to
cout << city1[4];
cout << city2[4];
// ...
cout << city1;
city2;
Finally, the corrected code is
#include <iostream>
#include <string>
#include <boost/algorithm/string.hpp>
using namespace std;
int main() {
string city1, city2;
cout << "Please enter your citys name" << std::endl;
cin >> city1;
cout << "Please enter your citys second name" << std::endl;
cin >> city2;
cout << city1 << std::endl;
cout << city2 << std::endl;
boost::to_upper(city1);
boost::to_upper(city2);
cout << city1 << std::endl << city2 << std::endl;
}
#include <cstdlib>
#include <crime>
#include <stream>
#include <cmath>
#include <iostream>
using namespace std;
int Kills;
int Deaths;
int main()
{
cout << "Please Enter The Amount Of Kills You Have: ";
cin >> Kills;
cout << "Please Enter The Amount Of Deaths You Have: ";
cin >> Deaths;
float answer = Kills / Deaths;
cout << "Your KD Is: " << answer;
//creating a .txt file
ofstream pctalk;
pctalk.open("KDA Tracker.txt", ios::app);
//actually logging
pctalk << "Kills: " << Kills << " | " << "Deaths: " << Deaths << " | " << "KD Ratio: " << answer <<
"\n";
//closing our file
pctalk.close();
return 0;
}
When code is run inside visual studio console does not exit right away. but when ran from the solution folder aka the exe it closes right after It inputs my deaths. Why is this happening?
Please put following statement just before return statement.
system("pause");
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.
When I enter this code and try to run it, it isn't working when the user selects option 1, to enter some text and a string to search for within their text. It outputs "enter text" and then "enter string to search" immediately after, without giving the user the chance to input some text. What is wrong?
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <algorithm>
using namespace std;
string s1, text;
int rand(int*);
int Array[100];
void sortArray(int[], int);
void showArray(const int [], int);
int main()
{
while (1)
// Menu to prompt user choice
{
char choice[1];
cout << endl;
cout << endl;
cout << "--MENU--" << endl;
cout << "1. Pattern Matching" << endl; // search for string within text
cout << "2. Sorting Techniques" << endl; // generate and then sort 10 random numbers
cout << "Enter your choice: " << endl;
cout << endl;
cin >> choice;
cout << endl;
if (choice[0] == '1') // string search option
{
cout << "Enter text:" << endl; // accept text from user
getline (cin, s1);
cout << "Enter string to search:" << endl; // accept string to search from user
getline (cin, text);
int pos = s1.find(text); // finds position where the string is located within text
if (pos >= 0)
{
cout << "Found '" << text << "'" << " at position " << pos + 1 << "." << endl;
}
else
{
cout << "Did not find text." << endl;
}
}
This is because cin >> choice reads part of the current input line for the choice entered by the user. The first getline() call reads the remaining part of the input line immediately following the choice entered by the user. You need to ignore the rest of the input line after the choice.
cin >> choice;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
You will also need to add #include <limits> to the beginning of your code in order to pull in numerical_limits.
It looks as though you are defining some sort of char array for the user response. I would tend to make that a non-zero integer type with an exception if the choice is neither 1 nor 2. There are also some shortcuts for output formatting that reduces lines of code. Also, you would want to include the standard string class to accept the string. Maybe try something like the following:
#include <string>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <algorithm>
using namespace std;
string s1, text;
int rand(int*);
int Array[100];
void sortArray(int[], int);
void showArray(const int [], int);
int main()
{
while (1)
// Menu to prompt user choice
{
int choice;
cout << "\n--MENU--\n"l;
cout << "1. Pattern Matching\n"; // search for string within text
cout << "2. Sorting Techniques\n"; // generate and then sort 10 random numbers
cout << "Enter your choice:\n";
cin >> choice+"\n";
if (choice == 1 && choice > 0 && choice != 0) // string search option
{
cout << "Enter text:" << endl; // accept text from user
getline (cin, s1);
cout << "Enter string to search:" << endl; // accept string to search from user
getline (cin, text);
int pos = s1.find(text); // finds position where the string is located within text
if (pos >= 0)
{
cout << "Found '" << text << "'" << " at position " << pos + 1 << ".\n";
}
else
{
cout << "Did not find text.\n";
}
}}}
I just started C++ recently and now I encounter a problem in a very simple program. I was successful inputting part_num and part_des (both type string), but the program skipped part_price and part_quant (type int). I could not enter values for these two last variables.How do I fix this problem? Thanks
#include <iostream>
using namespace std;
int main()
{
string part_num;
string part_des;
int part_price;
int part_quant;
cout<<"Please enter the part number: ";
cin>>part_num;
cout<<"\n"<<endl;
cout<<"Please enter the part description: ";
cin>>part_des;
cout<<"\n"<<endl;
cout<<"Please enter the part price: ";
cin>>part_price;
cout<<"\n"<<endl;
cout<<"Please enter the part quantity: ";
cin>>part_quant;
cout<<"\n"<<endl;
}
What Happens
Please test for yourself:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string my_string;
cout << "enter a string: ";
cin >> my_string;
cout << "you have entered: " << my_string << endl;
system("pause");
}
Results:
enter a string: one
you have entered: one
Press any key to continue . . .
enter a string: one two
you have entered: one
Press any key to continue . . .
Where is "two"? It remains in stream. When you try to get a number after this, this piece of text is automatically taken as your input, probably it rises an exception and your program suddenly ends.
How to Fix
Use something like getline.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string my_string;
cout << "enter a string: ";
getline (cin, my_string);
cout << "you have entered: " << my_string << endl;
system("pause");
}
This will eliminate the problem you're having.
enter a string: one two
you have entered: one two
Press any key to continue . . .
The code works fine. ( if you don't have whitespaces in there )
#include <iostream>
using namespace std;
int main()
{
string part_num;
string part_des;
int part_price;
int part_quant;
cout<<"Please enter the part number: ";
cin>>part_num;
cout<<"\n" << part_num <<endl;
cout<<"Please enter the part description: ";
cin>>part_des;
cout<<"\n" << part_des <<endl;
cout<<"Please enter the part price: ";
cin>>part_price;
cout<<"\n" << part_price <<endl;
cout<<"Please enter the part quantity: ";
cin>>part_quant;
cout<<"\n" << part_quant <<endl;
}
http://ideone.com/U31pb9
Just give yourself the output right away. To see what you were missing.
Fixed for whitespaces n stuff:
int main()
{
string part_num;
string part_des;
int part_price;
int part_quant;
cout<<"Please enter the part number: ";
getline( cin , part_num );
cout << "\n" << part_num <<endl;
cout<<"Please enter the part description: ";
getline( cin , part_des );
cout<< "\n" << part_des <<endl;
cout<<"Please enter the part price: ";
cin >> part_price;
cout << "\n" << part_price <<endl;
cout << "Please enter the part quantity: ";
cin >> part_quant;
cout << "\n" << part_quant <<endl;
}
Also think about using strings for the price and the quant as well and then convert them to integer if you want to keep using getline.
Try this, it should work:
#include <iostream>
using namespace std;
int main ()
{
int i;
cout << "Please enter an integer value: ";
cin >> i;
cout << "The value you entered is " << i;
cout << " and its double is " << i*2 << ".\n";
return 0;
}