C++ Line graph acting funky - c++

I am trying to create a line graph that displays temperatures that have been read from a local file. Currently, everything is working as intended except the graphical output.
Right now, my else statement for the negative numbers is not working correctly. In addition, some numbers appear to be shown and some do not.
Lastly, the numbers that are displaying are not showing the right number of '*". I know I am close but cannot crack the code...
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std
int main()
{
//Variable declarations
int numberOfStars;
int printStars;
int temp;
int count = 0;
string lineBreak = " | ";
ifstream tempData;
tempData.open("tempData.txt");
cout << "Temperatures for 24 hours: " << endl;
cout << " -30 0 30 60 90 120" << endl;
printStars = 0;
while (count < 24) {
tempData >> temp;
if (temp >= 0) {
numberOfStars = temp / 3;
cout << setw(4) << temp << setw(10) << lineBreak;
while (printStars < numberOfStars){
cout << '*';
printStars++;
}
cout << endl << endl;
}
else {
numberOfStars = temp / 3;
while (numberOfStars > printStars) {
cout << '*';
printStars++;
}
cout << setw(4) << temp << setw(10)<< lineBreak << endl << endl;
}
count++;
}
//Closing program statements
system("pause");
return 0;
Currently it is outputting:
Thanks for help in advance.

Oh just put your printStars = 0; in the while :)
And do numberOfStars = temp / 3 + 1; if you want to have one star for temp < 3.
EDIT: You can simplify a lot your code. You can create a string with n time a character very easily. Your code should look like this:
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
int main()
{
//Variable declarations
int numberOfStars;
int numberOfSpaces;
int temp;
int count = 0;
string lineBreak = " | ";
ifstream tempData;
tempData.open("data.txt");
cout << "Temperatures for 24 hours: " << endl;
cout << " -30 0 30 60 90 120" << endl;
while (count < 24) {
tempData >> temp;
numberOfStars = temp / 3 + 1;
if (temp >= 0) {
cout << setw(4) << temp << setw(10) << lineBreak;
cout << string(numberOfStars, '*');
}
else {
cout << setw(4) << temp;
numberOfSpaces = 7 + numberOfStars;
// Prevent any ugly shift
if (numberOfSpaces < 0)
{
numberOfSpaces = 0;
numberOfStars = -7;
}
cout << string(7 + numberOfStars, ' ');
cout << string(-numberOfStars, '*');
cout << lineBreak;
}
cout << endl << endl;
count++;
}
//Closing program statements
cin.get();
return 0;
}

Related

incorrect c++ output when executing

I am trying to replicate the following program but without including the cout function, this is the program with the cout function:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
bool incorrect = 1;
int temp;
int i = 0;
int peopleInGroup = 1;
string x = "";
int max;
stringstream ss;
cout << "Input the number of people in a group: 2 to ";
getline (cin, x);
ss << x;
ss >> max;
while(incorrect == 1)
{
i++;
cout << "Testing: " << i << endl;
peopleInGroup = 1;
while(peopleInGroup < max)
{
peopleInGroup++;
cout << "Remainder of (" << i << " /" << peopleInGroup << ") =" << (i % peopleInGroup) << " =" << (peopleInGroup - 1) << " ";
if((i % peopleInGroup) == (peopleInGroup - 1))
{
cout << "TRUE" << endl;
if(peopleInGroup == max)
{
incorrect = 0;
break;
}
else{}
}
else
{
cout << "FALSE" << endl;
break;
}
}
}
if(incorrect == 0)
{
cout << endl << "The answer is " << i;
}
else
{
cout << endl << "Unable to find the answer";
}
cin >> temp;
}
which when run shows
Input the number of people in a group: 2 to
and when I type 3, it comes out with 5 which is correct:
Input the number of people in a group: 2 to 3
...
The answer is 5
without cout, however when I do this the program comes out with an incorrect answer:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
bool incorrect = 1;
int temp;
int i = 0;
int peopleInGroup = 1;
string x = "";
int max;
stringstream ss;
cout << "Input the number of people in a group: 2 to ";
getline (cin, x);
ss << x;
ss >> max;
while(incorrect == 1)
{
i++;
peopleInGroup = 1;
while(peopleInGroup < max)
{
peopleInGroup++;
if((i % peopleInGroup) == (peopleInGroup - 1))
{
if(peopleInGroup == max)
{
incorrect = 0;
}
}
}
}
if(incorrect == 0)
{
cout << endl << "The answer is " << i;
}
else
{
cout << endl << "Unable to find the answer";
}
cin >> temp;
}
which is always
max - 1
for example, if I put in 3, it comes out with 2
you miss the break statement in the following code.
cout is not necessary but the break statement is to come out from the execution block. If you put the break statement as given in above code you will get the desired output

Unhandled exception at 0x012B1CA9

I am new to C++ and am trying to build a simple program that with the users input to proceed will generate a random left or right. I had the program working correctly until I added in the array to try and store each item as I have to output them as soon and the user would like to exit the loop. The program seems to compile fine but at run time I receive "Unhandled exception at 0x012B1CA9" Any help would be greatly appreciated.
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
int userSelection = 1;
const int MAX = '100';
int randNum(0);
int one (0);
int two (0);
int total(0);
int sel[MAX];
do
{
cout << "Press 1 to pick a side or 0 to quit: ";
cin >> userSelection;
for (int i = 1; i < MAX; i++)
{
srand(time(NULL));
sel[i] = 1 + (rand() % 2);
if (sel[i] == 1)
{
cout << "<<<--- Left" << endl;
one++;
total++;
}
else
{
cout << "Right --->>>" << endl;
two++;
total++;
}
}
} while (userSelection == 1);
cout << "Replaying Selections" << endl;
for (int j = 0; j < MAX; j++)
{
cout << sel[j] << endl;
}
cout << "Printing Statistics" << endl;
double total1 = ((one / total)*100);
double total2 = ((two / total)*100);
cout << "Left: " << one << "-" << "(" << total1 << "%)" << endl;
cout << "Right: " << two << "-" << "(" << total2 << "%)" << endl;
system("pause");
return 0;
};
You have a multi-character constant here... and the behavior doesn't go as expected...
Change this line
const int MAX = '100';
to
const int MAX = 100;
Note the removed single quotes.
And secondly, I will advice you to remove the Seed of the C random generator from the for loop because, you'll likely get the same values from the rand() if you always call it immediately after seeding...
But preferable use the algorithm from C++'s random header
Here is a corrected version of your original code....
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
int userSelection = 1;
const int MAX = 100; // <---changed
int randNum(0);
int one (0);
int two (0);
int total(0);
int sel[MAX];
do
{
cout << "Press 1 to pick a side or 0 to quit: ";
cin >> userSelection;
srand(time(NULL)); //< moved to here
for (int i = 0; i < MAX; i++) // <-- modified starting index
{
sel[i] = 1 + (rand() % 2);
if (sel[i] == 1)
{
cout << "<<<--- Left" << endl;
one++;
total++;
}
else
{
cout << "Right --->>>" << endl;
two++;
total++;
}
}
} while (userSelection == 1);
cout << "Replaying Selections" << endl;
for (int j = 0; j < MAX; j++)
{
cout << sel[j] << endl;
}
cout << "Printing Statistics" << endl;
double total1 = ((one / total)*100);
double total2 = ((two / total)*100);
cout << "Left: " << one << "-" << "(" << total1 << "%)" << endl;
cout << "Right: " << two << "-" << "(" << total2 << "%)" << endl;
system("pause");
return 0;
};
I think that it is basically good idea to read more about C data types and declaration. Your error:
const int MAX = '100' should be const int MAX = 100 without any quotes. C++ does implicit conversion from character literals to int.

How can I make this simple code more efficient without repeating the for loops for each year?

So this program produces a bar chart(very basic) showing the population growth at a 20 year intervals during the past 100 years.
It's working as intended, yet I feel there has to be a more efficient way of looping while obtaining the same result, instead of repeating the for loop for each year. I'd also like to keep the solution within the level displayed in the code (intro to C++)
The People.txt contains the following:
2000 4000 5000 9000 14000 18000
And this is the code
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int main()
{
ifstream inputFile; // File stream object
int number;
// Open the input file
inputFile.open("People.txt");
cout << "PRAIRIEVILLE POPULATION GROWTH\n" << "(each * represents 1000 people)\n";
//1910's bar
cout << "1910 ";
inputFile >> number;
for (int i = 1; i < number; i+=1000)
{
cout << "*";
}
cout << endl;
//1930's bar
cout << "1930 ";
inputFile >> number;
for (int i = 1; i < number; i+=1000)
{
cout << "*";
}
cout << endl;
//1950's bar
cout << "1950 ";
inputFile >> number;
for (int i = 1; i < number; i+=1000)
{
cout << "*";
}
cout << endl;
//1970's bar
cout << "1970 ";
inputFile >> number;
for (int i = 1; i < number; i+=1000)
{
cout << "*";
}
cout << endl;
//1990's bar
cout << "1990 ";
inputFile >> number;
for (int i = 1; i < number; i+=1000)
{
cout << "*";
}
cout << endl;
//2010's bar
cout << "2000 ";
inputFile >> number;
for (int i = 1; i < number; i+=1000)
{
cout << "*";
}
cout << endl;
// Close the file
inputFile.close();
return 0;
}
I think your code should look like the following:
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int main()
{
ifstream inputFile; // File stream object
int number;
// Open the input file
inputFile.open("People.txt");
cout << "PRAIRIEVILLE POPULATION GROWTH\n" << "(each * represents 1000 people)\n";
for(int y = 1910; y <= 2010; y += 20)
{
cout << y << ' ';
inputFile >> number;
for (int i = 1; i < number; i+=1000)
{
cout << '*';
}
cout << endl;
}
// Close the file
inputFile.close();
return 0;
}
Also note that quotation marks (string literal) have been changed to single quotes (character literal) for the asterisk. Operator << will be more efficient this way because it does not need to dereference the pointer a string literal in fact means, but it gets a mere character which fits in a register.
Something off the top of my head:
// Without error checking, something like this:
// Assuming it starts at year 1910, at 20 year intervals
int number = 0;
int year = 1910;
while (inputFile >> number) {
cout << year << " ";
for (int i = 0; i < number; i += 1000) {
cout << "*";
}
cout << "\n";
year += 2000;
}

Loop with an array to read integers from file, sort them according to range. C++

Right now Im trying to write a program that can take an input file that has a bunch of integers, and then show the amount of numbers in certain ranges. For example:
If the input file has 20, 30, 40, 50, 60. And the ranges are 1-20, 21-40, 41-60
The output would be
1-20: 1
21-40: 2
41-60: 3
etc.
Im new to programming, so Im just having a little problem with my code, I know im close. Im using arrays to store them, but I wasnt sure how to make an array that adapts to how many integers there are in a file. So this is what I got so far. (Just for this exercise, Im trying to make the max amount the array will store 100 integers).
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int ranges(int);
int main()
{
ifstream indata;
ofstream outdata;
indata.open("scores1.txt");
outdata.open("scoresoutput.txt");
if(!indata)
{
cout << "Unable to open file" << endl;
return 1;
}
int range1=0;
int range2=0;
int range3=0;
int range4=0;
int range5=0;
int range6=0;
int range7=0;
int range8=0;
const int max=100;
int score[max];
while(indata)
{
for(int i=0; i < max ; i++)
{
indata >> score[i];
indata.ignore(1);
if(0 <= score[i] <= 24)
{
range1++;
}
else if(25 <= score[i] <= 49)
{
range2++;
}
else if(50 <= score[i] <=74)
{
range3++;
}
else if(75<= score[i] <= 99)
{
range4++;
}
else if(100 <= score[i] <= 124)
{
range5++;
}
else if(125 <= score[i] <= 149)
{
range6++;
}
else if(150 <= score[i] <= 174)
{
range7++;
}
else if(175 <= score[i]<= 200)
{
range8++;
}
}
}
outdata << "Range" << setw(10) << "Number of Students" << endl <<
"0-24" << setw(10) << range1 << endl<<
"25-49" << setw(10) << range2 << endl <<
"50-74" << setw(10) << range3 << endl <<
"75-99" << setw(10) << range4 << endl <<
"100-124" << setw(10) << range5 << endl <<
"125-149" << setw(10) << range6 << endl <<
"150-174" << setw(10) << range7 << endl <<
"175-200" << setw(10) << range8 << endl;
return 0;
}
Right now, the output just displays 0-24 100. Any easier way to do this than what I am doing now?
Array is your good friend. Try to use an array to manage the counts:
int range_count[8];
for (int i = 0; i < 8; ++i) {
range_count[i] = 0;
}
and in the for-loop inside your while-loop:
for(int i = 0; i < max ; ++i) {
indata >> score[i];
indata.ignore(1);
if (score[i] >= 0 && score[i] <= 200) {
range_count[score[i] / 25]++; // since your range intervals is a constant 25.
}
}
and finally, in the output:
outdata << "Range" << setw(10) << "Number of Students" << endl;
for (int i = 0; i < 8; ++i) {
i * 25 << "-" << i * 25 + 24 << setw(10) << range_count[i] << endl;
}

Output desired number of columns

I was wanting to get some tips on how to get my program to output a desired number of columns by the user. For example, if the user enters 2 for termsPerLine, then the program should print the values generated by the Juggler series in two columns, or if the user enters 3 for termsPerLine, 3 columns and so forth. The output variable is firstTerm. Any assistance would be great.
#include <string>
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int ValidateInput(string Prompt);
int main()
{
int count;
double Odd;
double Even;
long long int firstTerm;
int noOfTerms;
int termsPerLine;
cout << "Program will determine the terms in a Juggler Series" << endl << endl;
firstTerm = ValidateInput("Enter the first term: ");
noOfTerms = ValidateInput("Enter the number of terms to calculate (after first): ");
termsPerLine = ValidateInput("Enter the terms to display per line: ");
cout << "First " << noOfTerms << " terms of JUGGLER SERIES starting with " << firstTerm << endl;
count = 1;
do
{
if (firstTerm % 2 == 0 )
{
firstTerm = pow(firstTerm , 0.5);
cout << setw(16) << firstTerm << endl;
count++;
}
if (firstTerm % 2 != 0 )
{
firstTerm = pow(firstTerm, 1.5);
cout << setw(16) << firstTerm << endl;
count++;
}
}
while (count <= noOfTerms);
cout << endl;
system("Pause");
return 0;
}
int ValidateInput( string Prompt)
{
int num;
cout << Prompt << endl;
cin >> num;
while ( num <= 0 )
{
cout << "Enter a positive number" << endl;
cin >> num;
}
return num;
}
Try this at the top of the loop:
if ((count % termsPerLine) == 0)
{
cout << "\n";
}
or this at the bottom of the loop:
if ((count % termsPerLine) == termsPerLine)
{
cout << "\n";
}
just Fix your loop as:
for (count = 1; count <= numOfTerm; count++)
{
if(firstTerm % 2 == 0)
firstTerm = pow(firstTerm, 0.5);
else
firstTerm = pow(firstTerm, 1.5);
if(count % termPerLine != 0)
cout << setw(15) << firstTerm;
else
cout << setw(15) << firstTerm << endl;
};