I'm trying to compile this but I get the error error: ‘setioflags’ was not declared in this scope which sounds like its trying to recognize it as a variable. This is an example I copied straight from my textbook, checked it over several times and can't find an error. Am I overlooking something? I'm on a mac if that makes a difference, I know the <conio.h> library doesn't work because of that, but <iomanip> is recognized
#include <iostream>
#include <iomanip>
using namespace std;
const int DISTRICTS = 4;
const int MONTHS = 3;
int main() {
int d, m;
double sales[DISTRICTS][MONTHS];
cout << endl;
for(d = 0; d < DISTRICTS; d++)
for(m = 0; m < MONTHS; m++)
{
cout << "Enter sales for district " << d+1;
cout << ", month " << m+1 << ": ";
cin >> sales[d][m];
}
cout << "\n\n";
cout << " Month\n";
cout << " 1 2 3";
for(d = 0; d < DISTRICTS; d++)
{
cout << "\nDistrict " << d+1;
for(m = 0; m < MONTHS; m++) // Display array values
cout << setiosflags(ios::fixed) // Not exponential
<< setioflags(ios::showpoint) // Always use poin
<< setprecision(2) // Digits to right
<< setw(10) // Field width
<< sales[d][m]; // Get number from array
} // end for(d)
cout << endl;
return 0;
}
You're looking for setiosflags. Note the extra s in there. Your spelling is different on the second call.
Related
I can't align the output of my program.
I want to keep the same names and get the right spacing.
The code is provided below.
I also tried using left but it still does not work.
The output I am expecting:
The output I am getting:
//taking name and votes recieved
for (i = 0; i < 5; i++)
{
cout << "Enter last name of candidate " << (i + 1) << ": ";
cin >> names[i];
cout << "Enter votes recived by " << names[i] << ": ";
cin >> votes[i];
}
//calculating total votes
for ( i = 0; i < 5; i++)
{
total = total + votes[i];
}
//calculating percentage of total votes for each candidate
for ( i = 0; i < 5; i++)
{
percent_of_total[i] = (votes[i] / total) * 100.0;
}
//checking winner
winner = names[0];
int most = 0;
for ( i = 0; i < 5; i++)
{
if (votes[i] > most)
{
most = votes[i];
winner = names[i];
}
}
cout << fixed << setprecision(2);
//dislaying
cout << "Candidte" << setw(20) << "Votes Recieved" << setw(20) << "% of Total Votes";
for (i = 0; i < 5; i++)
{
cout << endl;
cout << names[i] << setw(20) << votes[i] << setw(20) << percent_of_total[i];
}
cout << endl;
cout << "Total" << setw(20) << total;
cout << endl << "The winner of the Election is " << winner << ".";
return 0;
}
setw needs to be invoked before the field you wish to apply the fixed length to. That includes the names. If you want to keep the names left aligned you can use
std::cout << std::left << std::setw(20) << name /*<< [...]*/;
As a side note you should avoid using using namespace std;. The reason is that std contains a lot of names and you might use other libraries using the same names or use them yourself. std is fairly short and doesn't clutter up the code too much. A viable alternative is to use
using std::cout;
using std::cin;
using std::endl;
using std::setw;
using std::left;
// etc
for all the names you want to use.
another alternative is to invoke using namespace std in the function you want to use the std namespace.
#include <iostream>
void f()
{
using namespace std;
cout << "f() call" << endl;
}
int main()
{
// std not being used here by default
f();
return 0;
}
I'm trying to print the value of pointer array using for loop as usual, and I managed to print value stored in one object, but can't print the value stored in another object. My classes are defined in Predmet.h:
#include <iostream>
#include <string>
using namespace std;
class Predmet
{
public:
int numberOfItems;
string name;
Predmet();
~Predmet();
};
and Plaza.h:
class Plaza
{
public:
int length;
double x;
double y;
Plaza();
~Plaza();
};
My main.cpp looks like this:
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include "Plaza.h"
#include "Predmet.h"
using namespace std;
int main() {
int n, m;
int *numberOfBeaches;
Plaza *obj1;
Predmet *obj2;
cout << "Enter number of beaches (N): ";
cin >> n;
obj1 = new Plaza[n];
for (int i = 0; i < n; i++) {
cout << "Enter length and coordinates for " << i + 1 << ". beach: " << endl;
cin >> obj1[i].length;
cin >> obj1[i].x >> obj1[i].y;
}
cout << endl;
cout << "Enter number of items (M): ";
cin >> m;
obj2 = new Predmet[m];
numberOfBeaches = new int[n];
for (int i = 0; i < m; i++) {
cout << "Enter ordinal number of beach for " << i + 1 << ". item: ";
cin >> numberOfBeaches[i];
cout << "Enter how much of item you have and name of the item: ";
cin >> obj2[i].numberOfItems >> obj2[i].name;
}
int *p;
for (int i = 0; i < n; i++) {
p = find(numberOfBeaches, numberOfBeaches + n, i + 1);
if (*p == i + 1) {
for (int j = 0; j < m; j++) {
cout << i + 1 << ". " << obj1[i].x << " " << obj1[i].y << " D=" << obj1[i].length << " - predmeti: " << obj2[j].numberOfItems << " " << obj2[j].name << endl;
}
}
else {
cout << i + 1 << ". " << obj1[i].x << " " << obj1[i].y << " D=" << obj1[i].length << " - predmeti: " << endl;
}
}
delete[] obj1;
delete[] obj2;
delete[] numberOfBeaches;
system("pause");
return 0;
}
Everything was working until this point where I add printing for obj2[i].kolicina and obj2[i].opis, I get weird looking result as a print and this exception thrown, as you can see below:
What am I doing wrong? Thanks in advance.
EDIT:
After suggestions in the comments, I managed to fix the code (updated version above) to print it proper way, only when I have M > 1 (e.g. M = 2) I get duplicate printing of lines? How can I fix that?
The problem is with this line:
cout << i + 1 << ". " << obj1[i].x << " " << obj1[i].y << " D=" << obj1[i].duljina << " - predmeti: " << obj2[i].kolicina << " " << obj2[i].opis << endl;
obj2 is being defined as having m elements, yet you are using i, which has the values 0 <= i < n. I don't know what m's relation is to n, but that is certainly where you should start.
obj2contains melements:
obj2 = new Predmet[m];
brojPlaze contains n elements:
brojPlaze = new int[n];
you are looping over all Predmet in obj2:
for (int i = 0; i < m; i++) {
...
}
and inside the loop, you access element i of brojPlaze:
cin >> brojPlaze[i];
but i goes from 0 to m, and m can be greater than the n element brojPlaze contains. thus, you may access an element outside of the array, which can cause a lot of undesired effects...
I've been trying to overcome this problem for a few hours now and I seem to have one approach to the situation. It seems that the use of selection statements worked in creating the table necessary. Although there are formatting issues.
I'd like to know if there was a way to create the same table
using only nested for-loops as mentioned by our professor.
Are the selection statements necessary or can we implement a system of nested for loops to acquire the same results?
The image below is the required table:
But the image below is what I have:
Below is my code:
for (int i = 0; i <= numChoice; ++i)
{
if (i == 0)
{
for (int k = 1; k <= numChoice; ++k)
{
cout << " " << k;
}
cout << "\n";
}
else
{
cout << i << " | ";
for (int j = 1; j <= numChoice; ++j)
{
if (j*i <= 9)
{
cout << " " << j*i << "|";
}
else if (j*i > 9 && j*i <= 100)
{
cout << " " << j*i << "|";
}
else if (j*i > 99 && j*i <= 999)
{
cout << " " << j*i << "|";
}
}
cout << "\n";
for (int k = 0; k <= numChoice; ++k)
{
if (k == 0)
{
cout << "-|";
}
else
{
cout << "----|";
}
}
cout << "\n";
}
}
The following code uses no if else constructs. The formatting can be got by using setw, used for setting the width of integers.Following code produces perfect output.
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int i,j;
cout<<" "<<1;//5 space chars
for(i = 2;i <= 10;++i)
cout<<" "<<i;//4 space chars
cout<<endl;
cout<<" ----|";
for(i = 2;i <= 10;++i)
cout<<"----|";
cout<<endl;
for(i = 1;i <= 10;++i)
{
cout<<setw(2)<<i<<"|";
for(j = 1;j <= 10;++j)
cout<<setw(4)<<j*i<<"|";
cout<<endl;
cout<<" -|----";
for(j = 2;j <= 9;++j)
cout<<"|----";
cout<<"|----|";
cout<<endl;
}
return 0;
}
#FranticCode. I'm also in the same class as you and was having problems with this homework assignment as well. I still don't understand it, but I figured out how to manipulate Sumeet's code to give us correct format. The ONLY thing I am having a problem with now is adding an empty space AFTER the first multiplication table and before the menu redisplay. I'll share what I have and maybe you can figure it out. Still going to ask the professor to review chapter 5 because I would like to learn it rather than just submit the homework.
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
char userSelection;
int numForTable;
int col;
int row;
do
{
cout << "MENU" << endl
<< "a) Generate Multiplication Table" << endl
<< "q) Quit the program" << endl
<< "Please make a selection: ";
cin >> userSelection;
if (userSelection == 'a')
{
cout << "Please enter a number for your multiplication table: " << endl;
cin >> numForTable;
while (numForTable < 1 || numForTable > 10)
{
cout << "Please enter a number between 1 & 10." << endl;
cin >> numForTable;
}
cout << "\n"
<< "MULTIPLICATION TABLE: " << numForTable << "'s" << endl
<< "\n"
<< " " << 1;
for (col = 2; col <= numForTable; ++col)
cout << " " << col;
cout << endl;
cout << " ----|";
for (col = 2; col <= numForTable; ++col)
cout << "----|";
cout << endl;
for (col = 1; col <= numForTable; ++col)
{
cout << setw(2) << col << "|";
for (row = 1; row <= numForTable; ++row)
cout << setw(4) << col * row << "|";
cout << endl;
cout << " -|----";
for (row = 2; row <= numForTable - 1; ++row)
cout << "|----";
cout << "|----|";
cout << endl;
}
}
else if (userSelection != 'q')
{
cout << "Invalid Selection\n" << endl;
}
else if (userSelection == 'q')
{
cout << " You have chosen to quit the program. Thank you for using!" << endl;
}
}
while (userSelection != 'q');
//system("PAUSE");
return 0;
}
got curious to see if i could add the lines as easy as i claimed, it took a bit of fiddling, but here's the result (updated code below to also have lines).
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
int counter;
int counter2;
int amount;
cout << " |-----------------------------------------------------------|" << endl; // first line of table.
for(counter=1;counter<11;counter++){ // the 2 for lines create our 2 dimensional table
for(counter2=1;counter2<11;counter2++){
cout << " | " << setw(3) << counter*counter2; // setw(3) is a function of <iomanip>,
//setting minimum width to 3 for numbers.
}
cout << " |" << endl; // this here is being added to the end of each line and starts a new line.
cout << " |-----------------------------------------------------------|" << endl; // this is being inserted between each line, and starts a new line.
}
return 0;
}
Use the following construct:
for (int i=0; i<=numChoice; i++) // display first row of numbers
cout <<"\t" << i << "\t";
cout << "\n";
for (int i=0; i <=numChoice; i++) {
cout << i << "\t";
for (int j=0; j <=numChoice; j++)
cout << i*j << "\t";
cout << "\n";
}
I have my code here and it runs, however, when I try to output the percentage it just outputs 0, I've spent a long time trying to figure out what I'm missing and I'm clueless. Basically I'm trying to output the percent of votes for each candidate out of total votes. Any help would be appreciated. Here is my output;
Output display Also, im aware that the winner loops through every user until it reaches the end for some reason, still trying to work out the kinks.
Here is my code -
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
class candidatesElection
{
public:
string last;
float votePercent;
void winnerOfElection();
void outputDis();
int total = 0;
};
int main()
{
string lastName[5];
int amountOfVotes[5];
double percentTotal[5];
int total = 0;
int winnerNo = 0;
int winningCandidate;
string winningName;
for (int i = 0; i < 5; i++)
{
cout << "Enter the last name of the Candidate: " << endl;
cin >> lastName[i];
cout << endl;
cout << "Enter the votes received by the Candidate: " << endl;
cin >> amountOfVotes[i];
total += amountOfVotes[i];
cout << "Total number of votes is: " << total << endl;
}
for (int i = 0; i < 5; i++)
{
if (amountOfVotes[i] > amountOfVotes[winnerNo]) winnerNo = i;
amountOfVotes[i] = amountOfVotes[winnerNo];
}
for (int i = 0; i < 5; i++)
{
percentTotal[i] = (amountOfVotes[i] / total) * 100.0; // need to make it floating point
}
void outputDis();
{
cout << endl << left << setw(25) << "Candidate" << right << setw(25) << "Votes Received" << setw(25) << "% of Total Votes" << endl;
for (int i = 0; i < 5; i++)
cout << endl << left << setw(25) << lastName[i] << right << setw(25) << amountOfVotes[i] << setw(25) << percentTotal[i] << endl;
cout << endl << left << setw(25) << "Total" << right << setw(25) << total << endl;
for (int i = 1; i < 5; i++)
{
int winHigh = amountOfVotes[0];
string win = lastName[0];
if (amountOfVotes[i] > winHigh)
{
winHigh = amountOfVotes[i];
win = lastName[i];
}
cout << "The Winner of the Election is " << win << endl;
}
}
system("pause");
};
The coefficient amountOfVotes[i] / total in (amountOfVotes[i] / total) * 100.0 is evalated in integer arithmetic: i.e. any fraction is discarded.
So you end up with 0 * 100 for all cases where amountOfVotes[i] is less than total.
The solution is to rearrange the formula to 100 * amountOfVotes[i] / total;, or, even better 100.0 * amountOfVotes[i] / total; which will force evaluation in double precision floating point - you are in danger of overflowing an int which, on some systems, can have an upper limit as low as 32767.
That's not immediately obvious even when using a line-by-line debugger. But do use that debugger to work out the other "kinks".
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.