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.
Related
#include <iostream>
using namespace std;
int main()
{
int rows, count, num, space;//creating four variables to use for the loops
cout << "Enter number of rows: ";//prompting the user to input the number of rows
cin >> rows;
while (rows < 1 || rows>9)
{
cout << "Entry must be between 1 and 9. Please Re-enter the number of rows" << endl; //Asking the user to re-enter the number of rows if it was an invalid input
cin >> rows;
}
for (count = 1; count <= rows; count++) //for function that loops from 1-how many rows the user puts in
{
for (space= 1; space < rows; space++) //inner loop that loops from 1 to how many rows the user put in and outputs a space
{
cout << " ";
}
for (num = 1; num <= (2*count-1); num++)//the last row of the pyramid has one less than two time sthe number the user input so this loops unitl that is hit
{
cout <<count << " ";
}
cout << "\n"; //outputs a new line
}
system("pause");
return 0;
}
Looking at the code and you are making a right Angled Triangle of numbers and guessing you need an Isosceles Pyramid of numbers
Refer the code below:
#include <iostream>
#include <string>
bool makeIsoscelesPyramid(const unsigned int& size)
{
if( size > 9 )
{
std::cout<<"Please Enter a positive number less than 9"<<std::endl;
return false;
}
for (int i = 0; i < size; i++)
{
//Number of Space to input
auto nSpace = size - ( i );
std::cout << std::string( nSpace, ' ');
//Times the number is repeated 1, 3, 5 ...
auto nTimesNumber = (i * 2) + 1;
//'1'+i could only print till 9
std::cout << std::string( nTimesNumber, '1'+i) << std::endl;
}
return true;
}
int main()
{
unsigned int nRows = 0;
bool bReturn = false;
do
{
std::cout << "Enter number of rows: \t";
std::cin>>nRows;
std::cout<<std::endl;
bReturn = makeIsoscelesPyramid(nRows);
} while( !bReturn );
return 0;
}
Output:
Enter number of rows: 5
1
222
33333
4444444
555555555
So I have a program that reads in a certain number of keys. An example would be
5 1 10 21 9 6 21 11 13 16 20
The text file example would be
Java 2 linux 3 fear 0 pool 2 do 0 red 1 lock. 1 I 0 random 2
I want my program to start at Java and depending on first key, which in this case is 5, would move over 5 elements leading to "red". And so on and so on. However, I have managed to read in all the data and put them into arrays, I am just having trouble figuring out how to move the pointer in the array.
Code:
#include <iostream>
#include <fstream>
using namespace std;
struct pieces {
char word[5];
int jump;
} ;
// Main Function
int main ()
{
// declare variables
int wordCount[2];
int keyCount[2];
int numKeys, numKeys2;
int numWords;
int keyAmount = 1;
int wordAmount = 23;
int keyarr[11];
pieces cypher[wordAmount];
char filename[10];
ifstream inData;
int temp[10];
//prompt user for input file
cout << " Enter file name: ";
cin >> filename;
inData.open(filename);
if(inData.is_open());
{
// read in data
for ( numKeys = numWords = 0; numKeys < keyAmount; numKeys++){
// read in num of words and keys
inData >> wordCount[numKeys] >> keyCount[numKeys];
//read in words followed by jump key
for( numWords = 0; numWords < wordAmount; numWords++){
inData >> cypher[numWords].word >> cypher[numWords].jump;
}
// read in the actual keys
for( numKeys2 = 0; numKeys2 < wordCount[numKeys]; numKeys2++){
inData >> keyarr[numKeys2];
}
}
//print out data
for( int j = 0; j < numKeys; j++){
cout << wordCount[j] << "\n";
cout << keyCount[j] << "\n";
}
cout << "\n";
for ( int i = 0; i < wordAmount; ++i){
cout << cypher[i].word << " ";
cout << cypher[i].jump << " ";
}
cout << "\nKeys: " << "\n";
for(int k = 0; k < 11; k++){
cout << keyarr[k] << " ";
}
cout << "\n";
}
inData.close();
return 0;
}
I'm currently working on a program that outputs the number 1089 (i.e the Magic Number) of a three digit integer who's first digit is greater than its last. I have some code typed up, but am not receiving 1089, instead I'm receiving 891. Could anyone offer some explanation as to why.
//Uses a cout to inform user will be using the number 412 as an example.
//Uses a cout to explain to user the number needs to be reversed.
cout << "Alright, let's walk through an example, using the number 412." << endl;
int numExample = 412;
cout << "Please input 412" << endl;
cin >> numExample;
cout << "First, the number 412 is reversed." << endl;
//The following is done for reversing the number 412:
int reverseNum = 0, remainder = 0;
while (numExample)
{
remainder = numExample % 10;
reverseNum = (reverseNum * 10) + remainder;
numExample = numExample / 10;
}
cout << "The reverse of 412 is " << reverseNum << endl;
cout << "Next, we want to subtract the reverse of the original number from its reverse" << endl;
cout << "This gives us 198" << endl;
cout << "From there, we want to reverse 198." << endl;
//The same procedure is used to reverse 198
int numExample2 = 198;
cout << "Please enter 198" << endl;
cin >> numExample2;
int reverseNum2 = 0, remainder2 = 0;
while (numExample2)
{
remainder2 = numExample2 % 10;
reverseNum2 = (reverseNum2 * 10) + remainder2;
numExample2 = numExample2 / 10;
}
cout << "The reverse of 198 is " << reverseNum2 << endl;
int magicNumber = (reverseNum2 + numExample2);
cout << "Following that, we want to add 891 to 189 which gives us " << magicNumber << endl;
Try writing a function to handle this so your code is cleaner (It will also make it easier for people to help you!)
#include <iostream>
#include <cmath>
using namespace std;
int reverseDigit(int num); // For example purposes
int _tmain(int argc, _TCHAR* argv[])
{
int Number1,
Number2,
temp1,
temp2,
Result;
cout << "Enter the number 412: ";
cin >> Number1;
temp1 = reverseDigit(Number1);
temp1 = Number1 - temp1;
cout << "Enter the number 198: ";
cin >> Number2;
temp2 = reverseDigit(Number2);
Result = temp1 + temp2;
cout << "The magic number is: " << Result << endl;
return 0;
}
int reverseDigit(int num)
{
int reverseNum = 0;
bool isNegative = false;
if (num < 0)
{
num = -num;
isNegative = true;
}
while (num > 0)
{
reverseNum = reverseNum * 10 + num % 10;
num = num / 10;
}
if (isNegative)
{
reverseNum = -reverseNum;
}
return reverseNum;
}
I realize you're not working with negatives so you can remove that bit if you chose to use this, you can also expand on it... That being said this will make the actual " Reversing process " easier to work with and improve upon and read.
I have problem running my loops.
I have to run a program where user enter 4 digit number, and it will show how many zero appear. This program will run depends on how many times user want it to run. so at the beginning I prompt user to enter the number of times the program will run, then the actual program will run in For loop. but so far, I can only run it once. Can someone help me? Thank you. Here is my code
#include <iostream>
using namespace std;
int main()
{
int numberTimes;
cout << "How many times do you want to run this check?\n";
cin >> numberTimes;
for (int counter = 0; counter < numberTimes; counter++);
{
int positiveInteger;
//prompt user to enter a positive integer
cout << "Please enter a positive integer value.\n";
cin >> positiveInteger;
//conditional statement
while((positiveInteger <=0) || (positiveInteger > 9999))
{
cout << "Invalid Value!!! Please try again.\n";
cout << "Please enter a positive integer value.\n";
cin >> positiveInteger;
}
cout << "Processing the value " << positiveInteger << ".\n";
int zeroCount = 0;
int firstNumber = positiveInteger%10; //separate the first number
if (firstNumber == 0) //determine if the first digit is zero
{
zeroCount = zeroCount + 1;
}
int digitOne = positiveInteger/10; //omitted first digit number
int secondNumber = digitOne%10; //separate the second number
if (secondNumber == 0) //determine if the second digit is zero
{
zeroCount = zeroCount + 1;
}
int digitTwo = digitOne/10; //omitted the second number
int thirdNumber = digitTwo%10; //separate the third number
if (thirdNumber == 0) //determine if the third digit is zero
{
zeroCount = zeroCount + 1;
}
int digitThree = digitTwo/10; //omitted the third number
int fourthNumber = digitThree%10; //separate the fourth number
if (fourthNumber == 0) //determine if the fourth digit is zero
{
zeroCount = zeroCount + 1;
}
cout << "Your first digit number is " << firstNumber << ".\n";
cout << "Your second digit number is " << secondNumber << ".\n";
cout << "Your third digit number is " << thirdNumber << ".\n";
cout << "Your fourth digit number is " << fourthNumber << ".\n";
cout << "Number of zero appear in your integer is " << zeroCount << ".\n";
if (zeroCount % 2 == 0) //determine if the number is even or odd
{
cout << "Your number zero appear even times.\n";
} else
{
cout << "Your number zero appear odd times.\n";
}
}
cout << "You have run this program " << numberTimes << ".\n";
cout << "Thank you and good bye.";
return 0;
}
The problem is using ; at the end of for line.
for (int counter = 0; counter < numberTimes; counter++);
In such a case that you used, it iterate the loop but do nothing.
In the other word,it is equal to the below program now :
for (int counter = 0; counter < numberTimes; counter++)
{
}
You can also use this program : (Add complementary lines)
#include <iostream>
using namespace std;
int main()
{
int positiveInteger;
int numberTimes;
int digit;
cout << "How many times do you want to run this check?\n";
cin >> numberTimes;
while(numberTimes)
{
int zeroRepitation=0;
cout << "Please enter a positive integer value.\n";
cin >> positiveInteger;
if ((positiveInteger <=0) || (positiveInteger > 9999))
{
cout << "Invalid Value!!! Try again!\n";
continue;
}
for (int i=1;i<5;i++)
{
digit = positiveInteger % 10;
if (digit=0)
{
++zeroRepitation;
}
positiveInteger = positiveInteger / 10;
}
cout<<"Number of zeros in this number= "<<zeroRepitation;
--numberTimes;
}
You put a semicolon at the end of the for statement. This:
for (int counter = 0; counter < numberTimes; counter++);
should be this:
for (int counter = 0; counter < numberTimes; counter++)
I am trying to create a program that asks the user for a set of numbers, first asking for the quantity of numbers, then having them input all the numbers. The program then checks the numbers, and determines whether or not the numbers given are in ascending order or not. Then, simply print out "yes ascending" or "no not ascending" and print out the array on one line..So far, my code will just always say that "yes, this is an increasing array!" Please look below for my code. Thanks in advance!..
tested: 1 2 3 4 5 6 --> pass
1 3 5 2 4 6 --> fail (still says it is an ascending array)
#include <iostream>
#include <string>
using namespace std;
bool isAscending(int arr[], int size)
{
for (int i=0; i < size-1; i++)
{
if (arr[i] > arr[i+1])
{
return false;
}
}
return true;
}
int main()
{
int arraysize = 0;
string numbers;
cout << "Enter the size of the array: " << endl;
cin >> arraysize;
if (arraysize < 1)
{
cout << "ERROR: you entered an incorrect value for the array size!" << endl;
}
cout << "Enter the numbers in the array, separated by a space, and press enter: " << endl;
numbers += ' ' + numbers;
cin >> numbers;
int arr[arraysize];
if ( isAscending(arr, arraysize))
{
cout << "This IS an increasing array!" << endl;
}
else
{
cout << "This is NOT an ascending array!" << endl;
}
for (i = 0; i < arraysize - 1; i++)
{
cout << arr[i];
}
return 0;
}
You're reading in "numbers", which you created as type String.
cout << "Enter the numbers in the array, separated by a space, and press enter: " << endl;
numbers += ' ' + numbers;
cin >> numbers;
Your numbers+= line isn't doing anything except adding a space to your numbers string before your cin happens... I know what you're trying to accomplish, and that won't do it.
Then you're suddenly making an array which, without initializing is filled with garbage, and immediately running isAscending on it:
int arr[arraysize];
if ( isAscending(arr, arraysize)){....}
I advise you declare your array before receiving input, read the input line and process it into INTEGERS, and then add each integer to your array. Here is a (crude) correction of the first section of code in your main that just reads in whitespace separated integers and fills the array with them:
int main(){
int arraysize = 0;
int number = 0;
cout << "Enter the size of the array: " << endl;
cin >> arraysize;
if (arraysize < 1) // you should really have this loop until you get correct input
{ cout << "ERROR: you entered an incorrect value for the array size!" << endl; }
int* arr = new int[arraysize];
cout << "Enter the numbers in the array, separated by a space, and press enter: " << endl;
int i = 0;
while(i < arraysize){
cin >> number;
arr[i] = number;
i++;
};
if ( isAscending(arr, arraysize)){ cout << "This IS an increasing array!" << endl; }
else{ cout << "This is NOT an ascending array!" << endl;}
for (int i = 0; i < arraysize; i++){
cout << arr[i];
if( (i+1) == arraysize){ cout << ". ";}
else cout << ", ";
}
return 0;
}
If you're going to whine about somebody giving partial answers that only address the asker's question, maybe you should take the time to write something yourself.