I am new to C++ and need help with an assignment please.

lparker

Beta member
Messages
2
I am a new programming student and my assignment is to create a text file with a letter on the first line, and two double-digit numbers on the second line. Write a program using fstream that reads in your text file, creates variables to manipulate that input and outputs the following results to a different text file:

"The ASCII value of your character: " __ " is " ____"
(you will need to cast from a char to an int)
"The sum of " ___ " and " _____ " = " _____ "
(add the two numbers, display both the numbers and final result)
"The product of " ___ " and " _____ " = " _____"
(multiply the two numbers, display both the numbers and final result)

Here is what I have coded:

#include <fstream>

using namespace std;

int main()
{
ifstream inStream;
ofstream outStream;
char aChar='L';
int charVal = aChar; //to get ASCII value of variable


//Open the files
inStream.open("input.txt");
outStream.open("output.txt");

char first;
int firstnum, secondnum;
inStream >> first;
outStream << "The ASCII value of your character: " << (first) << " is " << (+charVal) << endl;

inStream >> firstnum >> secondnum;
outStream << "The sum of " << (firstnum) << (secondnum) << " is " << (firstnum + secondnum) << endl;

inStream >> firstnum >> secondnum;
outStream << "The product of " << (firstnum) << (secondnum) << " is " << (firstnum * secondnum) << endl;


return 0;
}


My input text doc is
L
24 25

and my output doc is coming up as
The ASCII value of your character: is 76
The sum of 083 is 83
The product of 083 is 0

Can someone explain to me what I am doing wrong and why this isn't working properly. I would really appreciate the help. :)
 
lparker said:
I am a new programming student and my assignment is to create a text file with a letter on the first line, and two double-digit numbers on the second line. Write a program using fstream that reads in your text file, creates variables to manipulate that input and outputs the following results to a different text file:

"The ASCII value of your character: " __ " is " ____"
(you will need to cast from a char to an int)
"The sum of " ___ " and " _____ " = " _____ "
(add the two numbers, display both the numbers and final result)
"The product of " ___ " and " _____ " = " _____"
(multiply the two numbers, display both the numbers and final result)

Here is what I have coded:

#include <fstream>

using namespace std;

int main()
{
ifstream inStream;
ofstream outStream;
char aChar='L'; // why is this set if you are reading 'first' from the file and using that for your character?
int charVal = aChar; //to get ASCII value of variable --> can remove this line
int sum = 0;
int product = 0;


//Open the files
inStream.open("input.txt");
outStream.open("output.txt");

char first;
int firstnum, secondnum;
inStream >> first; // if this is the character then shouldn't we be converting this to an int?
outStream << "The ASCII value of your character: (" << first << ") is " << static_cast<int>(first) << endl;

inStream >> firstnum >> secondnum;
sum = firstnum+secondnum;
outStream << "The sum of " << firstnum << " and " << secondnum << " is " << sum << endl;

inStream >> firstnum >> secondnum; // can remove this line because you already have the inputs (assuming they haven't changed)
product = firstnum*secondnum;
outStream << "The product of " << firstnum << " and " << secondnum << " is " << product << endl;

inStream.close(); //should close files when done with them
outStream.close();


return 0;
}


My input text doc is
L
24 25

and my output doc is coming up as
The ASCII value of your character: is 76
The sum of 083 is 83
The product of 083 is 0

Can someone explain to me what I am doing wrong and why this isn't working properly. I would really appreciate the help. :)

IDK, i made a few minor changes that may help. You can try them and see if it makes a difference. If you're still getting the wrong answers then there are probably errors while reading from the input file. You should do some cout's after you read the variables to be sure that you are getting the right ones. (just for testing purposes--> you can remove them after)
 
Back
Top Bottom