I have a homework question. I need help displaying my results in this format:
Price1 Price2 Price3
Price4 Price5 Price6
Price7 Price8 Price9
How can I display the results in the desired format in a cout statement? Here's my code:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
// use a constant Declaration
const int SIZE = 9;
// use a variable Declaration
float prices[SIZE];
cout << fixed << setprecision(2);
// prompt user to enter all 9 values :
for(int i = 0; i < SIZE; i++)
{
cout << "Enter the value: " << i + 1 << "-> ";
cin >> prices[i];
}
cout << "\n----------------------------------------------------\n";
// Display all values in the array
for(int i = 0; i < SIZE; i++)
{
cout << "value Entered " << i + 1 << "\t\t " << right << setw(7) << prices[i] << endl;
}
}
This is the output when running the code:
Enter the value: 1-> 21
Enter the value: 2-> 34
Enter the value: 3-> 54
Enter the value: 4-> 12
Enter the value: 5-> 65
Enter the value: 6-> 34
Enter the value: 7-> 76
Enter the value: 8-> 88
Enter the value: 9-> 90
----------------------------------------------------
value Entered 1 21.00
value Entered 2 34.00
value Entered 3 54.00
value Entered 4 12.00
value Entered 5 65.00
value Entered 6 34.00
value Entered 7 76.00
value Entered 8 88.00
value Entered 9 90.00
--------------------------------
Process exited after 16.86 seconds with return value 0
Press any key to continue . . .
Here try this
#include <iostream>
using namespace std;
int main()
{
// use a constant Declaration
const int SIZE = 9;
// use a variable Declaration
float prices[SIZE];
// prompt user to enter all 10 values :
for (int i = 0; i < SIZE; i++)
{
cout << "Enter the value: " << i + 1 << "-> ";
cin >> prices[i];
}
cout << "\n----------------------------------------------------\n";
// Display all values in the array
int index = 0;
while (index < SIZE)
{
for (int ctr = 0; ctr < 3; ++ctr)
{
cout << "values Entered " << index + 1 << " " << prices[index] << "\t\t";
++index;
}
cout << endl;
}
}
Related
I have this section of code for generating an array of randomly generated and sorted ints, but the nosearch cin quits the program. Calling Cin.ignore() & cin.clear() before and/or after this line does not correct this, no error message is shown, the program just quits as soon as I hit enter on that section. This is the only statement to do this.
#include <iostream>
#include <stdlib.h>
#include <ctime>
#include "search.hpp" //a function called sarch is here, program quits before sarch is called.
using namespace std;
int main () {
srand(time(0));
int tenn;
int minrange;
int maxrange;
cout << "Enter the number of integers: ";
cin >> tenn;
cout << endl << "Enter the lower limit: ";
cin >> minrange;
cout << endl << "Enter the upper limit: ";
cin >> maxrange;
int *arr = new int(tenn*10);
for (int i = 0; i < tenn*10; i++){
*(arr+i) = (rand() % maxrange) + minrange;
}
cout << "list, unsorted: " << endl;
for (int i = 0; i < tenn*10; i++){
cout << *(arr+i) << " ";
}
cout << endl;
for (int i = 0; i < tenn*10; i++){
int low = i;
for (int j = i; j < tenn*10; j++){
if (*(arr+j) < *(arr+low)){
low = j;
}
}
int temp = *(arr+i);
*(arr+i) = *(arr+low);
*(arr+low) = temp;
}
cout << "sorted list: " << endl;
for (int i = 0; i < tenn*10; i++){
cout << *(arr+i) << " ";
}
cout << endl;
int nosearch;
cout << "Enter the number of values you wish to search for: " << endl;
//The next cin will fail
cin >> nosearch;
cout << "v";
//more code here...
I use a makefile to compile the code, and when I run it in terminal, this is what happens:
>./Needle.exe
Enter the number of integers: 2
Enter the lower limit: 0
Enter the upper limit: 10
list, unsorted:
9 4 6 2 4 5 2 9 1 4 7 8 3 4 9 4 6 7 9 9
sorted list:
1 2 2 3 4 4 4 4 4 5 6 6 7 7 8 9 9 9 9 9
Enter the number of values you wish to search for:
3
>
I can only assume something I did above triggered this error, which is why I included so much of the code.
I'm trying to sort out the grades, but subscripts will not go to their right spot. I've tried to remove int in each for loop, except the first one, but there was an error saying
‘sub’ was not declared in this scope
And the output looks like this
Enter 15 exam grades: 90 80 70 60 50 91 81 71 61 51 92 82 72 62 52
Students who earned an A: 3 5 9 11 14
Students who earned a B:
Students who earned a C:
Students who earned a D:
Students who earned a F: 1 2 4 6 7 8 10 12 13 15
I assume there was something wrong with the compiler, but I'm sure.
Here is my code
#include <iostream>
using namespace std;
int main()
{
/* Declaring variables */
const int SIZE = 15;
int grades[SIZE];
/* Message Prompt */
cout << "Enter " << SIZE << " exam grades: ";
for (int sub = 0; sub < SIZE; sub++)
cin >> grades[SIZE];
/* Results Display for A */
cout << "Students who earned an A: ";
for (int sub = 0; sub < SIZE; sub++)
{
if (grades[sub] >= 90)
{
cout << sub + 1 << " ";
}
}
cout << endl;
/* Results Display for B */
cout << "Students who earned a B: ";
for (int sub = 0; sub < SIZE; sub++)
{
if ((grades[sub] <= 89) && (grades[sub] >= 80))
{
cout << sub + 1 << " ";
}
}
cout << endl;
/* Results Display for C */
cout << "Students who earned a C: ";
for (int sub = 0; sub < SIZE; sub++)
{
if ((grades[sub] <= 79) && (grades[sub] >= 70))
{
cout << sub + 1 << " ";
}
}
cout << endl;
/* Results Display for D */
cout << "Students who earned a D: ";
for (int sub = 0; sub < SIZE; sub++)
{
if ((grades[sub] <= 69) && (grades[sub] >= 60))
{
cout << sub + 1 << " ";
}
}
cout << endl;
/* Results Display for F */
cout << "Students who earned a F: ";
for (int sub = 0; sub < SIZE; sub++)
{
if (grades[sub] <= 59)
{
cout << sub + 1 << " ";
}
}
cout << endl;
return 0;
}
This line is wrong:
for (int sub = 0; sub < SIZE; sub++) cin >> grades[SIZE];
It should be grades[sub] instead.
Attempting to create my first program Expected behaviour:
1. User inputs an integer (lets say 7)
2. User continues to input same integer more times (lets say 4 total)
3. User inputs 8
4. Program then prints ("the count of 7 was 4")
5. User continues to enter 8 (lets say a 8 was entered a total of 11 times)
6. User enters 73
7. Program then prints ("the count of 8 was 11")
After step 4, the program gets stuck and any inputs yeild "7 was entered 0 times"
#include <iostream>
int main(){
int inputtedvalue = 0;
int firstvalue = 0;
int cnt = 0;
if(std::cin >> firstvalue){
++cnt;
while(std::cin >> inputtedvalue){
if(inputtedvalue == firstvalue)
++cnt;
else{
std::cout << "the count of " << firstvalue <<" is " << cnt << std::endl;
inputtedvalue = firstvalue;
cnt= 1;
}
}
}
}
I was looking over your example, and i believe the following might get you back in the right direction.
using namespace std;
int main(){
int inputtedvalue = 0;
int firstvalue = 0;
int cnt = 0;
cout << "Enter Starting value."<< std::endl;
if (cin >> firstvalue){
cout << "New count " << firstvalue <<" of " << cnt << endl;
while(cin >> inputtedvalue)
{
if(inputtedvalue != firstvalue)
{
cout << "It Doesn't Match" << endl;
firstvalue = inputtedvalue;
}
else
{
cout << "It Matches" << endl;
}
}
}
A given file contains pairs of <two-digit number, amount>. Then take a toss-up two-digit number (called X), and compute the win/loss amount. The win/loss rule is if the input number matches X, then it’s a win and the winning total is (amount * 70); otherwise, it’s a loss of (-amount).
For example: [ticket.txt]
09 10
13 15
25 21
If the toss-up number is 09, the win/loss amount of the ticket is (10 * 70 - 15 - 21)
If the toss-up number is 42, the win/loss amount of the ticket is (-10 - 15 - 21).
This is my beginner project. I stuck at calculating the win amount and lost amount.
This is my problem
#include <iostream>
#include <fstream>
using namespace std;
int line1[100]; // array that can hold 100 numbers for 1st column
int line2[100]; // array that can hold 100 numbers for 2nd column
int main()
{
int winNum, winAmount, lostAmount;
int num = 0; // num start at 0
ifstream inFile;
inFile.open("Ticket.txt"); //open File
if (inFile.fail())
{
cout << "Fail to open the file" << endl;
return 1;
}
cout << "Numbers from File: " << endl;
while (!inFile.eof()) // read File to end of file
{
inFile >> line1[num]; // read first column, the first column is the number that user choosing
inFile >> line2[num]; // read second column, the second column is the amount of money that user paying
cout << "\n" << line1[num] << "\t" << line2[num];
++num;
}
inFile.close();
cout << endl;
cout << "Enter the toss-up number: "; // enter the win number
cin >> winNum;
if (line1[num] == winNum)
{
winAmount = line2[num] * 70; // number user choose = win number, winAmount = winAmount * 70 - lostAmount
cout << winAmount;
}
else
{
lostAmount =- line2[num]; //number user choose != win number, the amount will be -lost amounts
cout << lostAmount;
}
cout << endl << endl;
system("pause");
return 0;
}
You can see result at the end of the code
#include <iostream>
#include <fstream>
using namespace std;
int line1[100]; // array that can hold 100 numbers for 1st column
int line2[100]; // array that can hold 100 numbers for 2nd column
int main()
{
int winNum, winAmount = 0, lostAmount = 0, result = 0;
int num = 0; // num start at 0
ifstream inFile;
ifstream inFile2;
int rowNumber = 0;
string line;
inFile.open("Ticket.txt"); //open File
inFile2.open("Ticket.txt");
if (inFile.fail())
{
cout << "Fail to open the file" << endl;
return 1;
}
while (getline(inFile2, line))
++rowNumber;
cout << "Number of lines in text file: " << rowNumber << "\n";
int myArray[rowNumber][2];
for(int i = 0; i < rowNumber; i++)
for(int j = 0; j < 2; j++)
inFile >> myArray[i][j];
cout << "Numbers from File: " << endl;
for(int i = 0; i < rowNumber; i++)
{
for(int j = 0; j < 2; j++)
{
cout << myArray[i][j] << " ";
}
cout << "\n";
}
cout << endl;
cout << "Enter the toss-up number: "; // enter the win number
cin >> winNum;
for(int i = 0; i< rowNumber; i++)
{
if (myArray[i][0] == winNum)
{
winAmount = myArray[i][1] * 70; // number user choose = win number, winAmount = winAmount * 70 - lostAmount
}
else
{
lostAmount = lostAmount + myArray[i][1]; //number user choose != win number, the amount will be -lost amounts
}
}
result = winAmount - lostAmount;
cout << result;
cout << endl << endl;
system("pause");
return 0;
}
When you test line1[num] == winNum (and all the operations carried out after that) you're using the value of num that you modified with ++num;, this means you're working with empty or not meaningful values for both line1 and line2. For example, if the 3 rows of values showed in your "ticket.txt" are used, they are stored in postions 0, 1 and 2 of the arrays, while the num has a value of 4 at the end.
If I understood what you are trying to achieve, you should put the if-else statement in a for loop that goes from 0 to num, and then every operation on line1 and line2 should be done with the looping variable as an index.
Also, move the cout just after the loop if you want only total amounts to be displayed.
//Page 215, #2, by Jeremy Mill
//program taken in 7 values, displays them, and then sorts them from highest to lowest, and displays them in order.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
//Define the variable we will need
const int arraySize = 6;
double dailySales[arraySize];
//Now let's prompt the user for their input
for (int a=0 ; a <= arraySize; a++ )
{
cout << "Please enter sale number " << a+1 << " :";
cin >> dailySales[a];
}
//Now we display the output of the array
cout << "\n\nSale Number" << setw( 13 ) << "Value" << endl;
for ( int i =0; i <= arraySize; i++ )
cout << setw( 5 ) << i << setw( 14 ) << dailySales[ i ] << endl;
//Now we sort using a bubble sort
for(int b = 0; b<=arraySize; b++)
for(int c = arraySize-1; c>=b; c--) {
if(dailySales[c-1] > dailySales[c]) { // if out of order
// exchange elements
int t = 0;
t = dailySales[c-1];
dailySales[c-1] = dailySales[c];
dailySales[c] = t;
cout << "it ran";
}
}
cout << "Now we can display the array again! \n\n\n" << endl << dailySales[6] << endl;
//Now we display the output of the sorted array
cout << "\n\nSale Number" << setw( 13 ) << "Value" << endl;
for ( int d = 0; d <= arraySize; d++ )
cout << setw( 5 ) << d << setw( 14 ) << dailySales[ d ] << endl;
cin.clear(); //clear cin
cin.sync(); //reinitialize it
cout << "\n\nPress Enter to end the program\n\n"; //display this text
cin.get(); //pause and wait for an enter
return 0;
} // end main
Output:
Please enter sale number 1 :1
Please enter sale number 2 :2
Please enter sale number 3 :3
Please enter sale number 4 :4
Please enter sale number 5 :5
Please enter sale number 6 :6
Please enter sale number 7 :7
Sale Number Value
0 1
1 2
2 3
3 4
4 5
5 6
6 7
Now we can display the array again!
7
Sale Number Value
0 1
1 2
2 3
3 4
4 5
5 6
6 2.97079e-313
Press Enter to end the program
Why is it that the last 'value' is not 7, but that number in sci. notation??
Be carreful when looping through your array. The loop has to start from 0 to size-1.
So instead of using less-than-or-equal:
for (int a = 0; a <= arraySize; a++)
You should use less-than:
for (int a = 0; a < arraySize; a++)
The array size is declared to be 6 elements and then 7 elements [0...6] are placed in it.
Redeclare array size to be 7 and then change all the <= to < in the loops.