Go Back   Computer Forums > General Computing > Programming
Click Here to Login
Join Computer forums Today


Reply
 
Thread Tools Search this Thread Display Modes
 
Old 10-29-2006, 12:05 AM   #1
Beta Member
 
Join Date: Oct 2006
Posts: 2
Default I am new to C++ and need help with an assignment please.

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 is offline   Reply With Quote
Old 10-29-2006, 12:13 PM   #2
Fully Optimized
 
Š88's Avatar
 
Join Date: May 2005
Posts: 4,775
Send a message via MSN to Š88
Default Re: I am new to C++ and need help with an assignment please.

Quote:
Originally Posted by lparker
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)
__________________
Killing under the cloak of war is no different than murder

Kein Alkohol ist auch keine Lösung!
Š88 is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT -5. The time now is 06:17 AM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0