Console is exiting right after getting inputs instantly - c++

#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");

Related

C++: Creating a .txt file output, but the program skips the second input

I'm trying to build a program that will create a .txt file and store some basic data within. Currently, the setup is pretty basic, but I've run into a problem where after inputting one's full name, the terminal seems to skip the second cin statement, (cin >> number1). I'm not certain why, and if anyone sees what might be the issue here, I would really appreciate the feedback.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ofstream fout1;
int number1;
string name;
fout1.open("numbers.txt", ios::out);
if (fout1.fail())
{
cout << "Failed to open file." << endl;
exit(1);
}
cout << "Input your full name: ";
cin >> name;
cout << "Enter a value: ";
cin >> number1;
fout1 << name << endl << number1 << endl;
fout1.close();
cout << "Your file 'numbers.txt' has been created!";
}
In addition, from every source I've seen, it seems that commanding .open on a file that doesn't yet exit will create the file in question, but I'm not able to confirm this due to the previous problem. Can anyone verify that my understanding here is correct?
Thank in advance.
As user:6752050 and user:65863 said, using getline(cin, name) solved the problem. In all my tests, the input name had a space somewhere in it which was causing some messy things to go on behind the scenes when cin was looking for a string with no spaces.
Solved code should read as follows:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ofstream fout1;
int number1;
string name;
fout1.open("numbers.txt", ios::out);
if (fout1.fail())
{
cout << "Failed to open file." << endl;
exit(1);
}
cout << "Input your full name: ";
getline(cin,name);
cout << "Enter a value: ";
cin >> number1;
fout1 << name << endl << number1 << endl;
fout1.close();
cout << "Your file 'numbers.txt' has been created!";
}

Xcode won't recognize/read file properly

I built a program in c++ to calculate the amount of gas money I owe to my dad for my summer job at Domino's. What the program does is read two lines from a file in a do while loop, one for the starting mileage that day and one for the ending mileage of that day. However I just can't get the program to read the file properly. I'm positive I opened it correctly and put it in the right spot (same folder as the .cpp file) but it just won't read it properly. Any ideas?
#include <iomanip>
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream inputFile;
inputFile.open("sample.txt");
int days; // number of days Alisha worked
int count = 1;
int totalMileage = 0;
int addMileage;
int startMileage;
int endMileage;
double avgMileage;
double moneyOwed;
double realOwed;
const double PER_MILE = 0.08125;
cout << endl;
cout << "How many days did Alisha work?" << endl;
cin >> days;
days++;
do
{
inputFile >> startMileage;
cout << "The starting mileage for day " << count << " is " << startMileage << endl;
inputFile >> endMileage;
cout << "The ending mileage for day " << count << " is " << endMileage << endl;
addMileage = endMileage - startMileage;
totalMileage = totalMileage + addMileage;
count++;
cout << endl;
}
while (count != days);

Can someone help me solve this issue?

//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.

C++ "Error: a nonstatic member reference must be relative to a specific object"

I'm trying to include experiencecalculator from a class but I get this Error: a nonstatic member reference must be relative to a specific object.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <stdio.h>
#include <iomanip>
#include <windows.h>
#include "ExpCalc.h"
#include <algorithm>
#include <string>
#include <cctype>
using namespace std;
int main() {
cout << "Pick which calculator you would like to use by typing the correct "
"number.\n";
cout << "1. Experience Calculator" << endl;
// cout << "" Insert other calculators and there number here.
// cout << ""
int choice;
cin >> choice;
if (choice == 1) {
ExpCalc::ExperienceCalculator;
}
}
The class I am taking it from is:
ExpCalc.h
class ExpCalc
{
public:
ExpCalc();
int ExperienceCalculator;
};
ExpCalc.cpp
#include "stdafx.h"
#include "ExpCalc.h"
#include <iostream>
#include <string>
#include <stdio.h>
#include <iomanip>
#include <windows.h>
#include <algorithm>
#include <string>
#include <cctype>
using namespace std;
ExpCalc::ExpCalc() {}
int ExperienceCalculator() {
double timetotal;
double timeperinv;
double xptotal;
double xpitem;
double amount;
double perinv;
double totalinv;
double costper;
double costtotal;
SetConsoleTitle(TEXT("Runescape Skill Calculator"));
cout << "=+=+=+=+=+=+=+=+=+=+=+=+=+=Runescape Skill "
"Calculator=+=+=+=+=+=+=+=+=+=+=+=+=+=" << endl;
cout << endl;
cout << "How much experience do you want to get?" << endl;
cin >> xptotal;
cout << endl;
cout << "How much does it cost per item?" << endl;
cin >> costper;
cout << endl;
cout << "How much experience do you get per item?" << endl;
cin >> xpitem;
cout << endl;
cout << "How many items can you process in one inventory?" << endl;
cin >> perinv;
cout << endl;
cout << "How long does it take to process one inventory of items?" << endl;
cin >> timeperinv;
system("CLS");
amount = xptotal / xpitem;
totalinv = amount / perinv;
timetotal = totalinv * timeperinv;
costtotal = amount * costper;
cout << "=+=+=+=+=+=+=+=+=+=+=+=+=+=Runescape Skill "
"Calculator=+=+=+=+=+=+=+=+=+=+=+=+=+=" << endl;
cout << endl;
std::cout << std::setprecision(1) << fixed;
cout << "The amount of items that you will need to process is: \n" << amount
<< endl;
cout << endl;
cout << "The total amount of inventories to process all items is: \n"
<< totalinv << endl;
cout << endl;
cout << "The total time it will take to complete processing all items is:\n"
<< timetotal << endl;
cout << endl;
cout << "The total cost of the items will be: \n" << totalinv << endl;
cout << endl;
cout << "The total amount of inventories to process is: \n" << totalinv
<< endl;
cout << endl;
cout << "=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+==+=+==+=+==+=+==+=+==+=+=+=+=+=+=+="
"+=+=+=+=+=+=+=" << endl;
system("PAUSE");
return 0;
};
Any help will be much appreciated!
Your H file describes ExperienceCalculator as int field. Your CPP file describes ExperienceCalculator as a free function (even not a method of ExpCalc). So I suspect that you have to do the following amends:
H file:
int ExperienceCalculator(); // parenthesis to be added
CPP file:
int ExpCalc::ExperienceCalculator() { // class name ExpCalc to be added
main file:
if (choice == 1) {
ExpCalc exp_calc; // instantiate the class
exp_calc.ExperienceCalculator(); // make a call to non-static method
}
Alternatively, you can make the method as static but let make one step at a time. Happy coding!

outFile C++ not writing to output text

Down below is my incomplete program. I am having problems with writing to a text file. For example I want to write the number of snow days to a text file, but nothing shows up in the textfile when I debug in VS 2010. It does display my info and name, but nothing else works. It wont write anything after that. its NOT writing to a text file.
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
const string INFORMATION = "College Class";
const string MY_NAME = "Tom Hangler";
int main(void)
{
ofstream outFile;
int numberOfSnowDays;
int greatestSnowDay;
int dayNumber;
double amounttOfSnow;
outFile.open("Ex1Out.txt");
outFile << setw(51) << INFORMATION << endl << setw(48) << MY_NAME << endl;
cout << "Please enter num of days it snowed: " << endl;
cin >> numberOfSnowDays;
outFile << setw(10) << "Number of days of snow is: " << setw(10) << numberOfSnowDays;
int index;
//Problem 1 for-loop
for (index = 0; index < numberOfSnowDays; index++)
{
cout << "Enter day: " << endl;
cin >> dayNumber;
cout << "Enter amount of snow: " << endl;
cin >> amountOfSnow;
};
return 0;
}
here is what my output displays:
College Class (centered)
Tom Hangler (centered)
If i try to write anything after this, Nothing is written ever to the output file. And the output text file IS in my VS project that contains my .cpp file. I added the text file to project.
Try closing the stream at the end of the function, it looks like the data isn't getting flushed.
outFile.close();
Your code compiles and works on gcc 4.4.5 (apart from typo in amounttOfSnow).
Is it possible that you are looking at an old Ex1Out.txt file ?
Its most likely created in the Release or Debug subdirectory in your project, not where the .cpp files are.
in your for loop, you only collect the amount of snow, but you don't write it to the text file.
Do you want to do something like this?
...
for (index = 0; index < numberOfSnowDays; index++)
{
cout << "Enter day: " << endl;
cin >> dayNumber;
cout << "Enter amount of snow: " << endl;
cin >> amountOfSnow;
// next line is new:
outFile << "Day#: "<< dayNumber << ", snow: "<< amountOfSnow<<endl;
};
outFile.close()
...