this is what the assignment says :

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
a data text file in c:\ called data.txt
inside the file i have the letter "h"
on the 1st line
on the 2nd line is 54 & 66
it reads like this
----------------------------------------------------------------------------------------------------------
h
54 66
----------------------------------------------------------------------------------------------------------
here is the code i have so far

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

int main ()
{

                                //variables declared for fstream
                                    ifstream indata;
                                    ofstream outdata;


                                 //files to open and to close
                                 indata.open("c:\\data.txt");
                                 outdata.open("c:\\results.txt");

                                 char line1; 
                                 int line2a, line2b; 



                indata >> data.txt = line
               	cout << " The ASCII value of your character: " __ " is " ____" << endl;





return 0;


}

and now im stuck ...
i do believe i have the right declaration , i dont know how to make the this run any more than what i got

can any one show me the code im suppose to be using and why ???

Recommended Answers

All 12 Replies

#include <iostream>
#include <fstream>

using namespace std;

int main ()
{

	//variables declared for fstream
	ifstream indata;
	ofstream outdata;


	//files to open and to close
	indata.open("c:\\data.txt");
	outdata.open("c:\\results.txt");

	char line1; 
	int line2a, line2b; 
	char val1[4],val2[4];	
	char delim = ' ';

	indata>>line1;
	cout << " The ASCII value of your character is " << (char)line1 << endl;
	;

	indata.getline(val1,4,delim);
	line2a = atoi(val1);

	indata.getline(val2,4,delim);
	line2b = atoi(val2);

	int sum = line2a + line2b;
	int product = line2a * line2b;

	outdata<<line1;
	outdata<<'\n';
	outdata << "The sum of the two numbers " << line2a << " and " << line2b << " is = " << sum << '\n';	
	outdata << "The product of the two numbers " << line2a << " and " << line2b << " is = " << product << '\n';	

	indata.close();
	outdata.close();

	return 0;

}

Does this solve your problem??

can any one show me the code im suppose to be using and why ???

Isn't that a "please do my homework for me" question? We frown upon that. You need to ask for help and explain why you are stuck -- what is it you don't understand.

And learn to format your code properly. It's very hard to read.

Does this solve your problem??

It actually created more problems. Since you did it for him, he'll flunk his test because he didn't learn anything. He's now going to have to move back with his parents and get a job at Burger King because he can't program. His career is over before it even starts. Hope you're happy... :)

Not only that, your program didn't follow the instructions so even you flunked the assignment.

Create a text file with a letter on the first line, and two double-digit numbers on the second line.

You need to learn to test your code so you don't give people bad advice. :icon_wink:

ok

sorry let me explain it a little different


ok i knew what i wanted out of the code i wanted to read data from the text i wasnt sure if i had the right commands.


1-the main problem was how to get it to read from the text file , no where in my book does it have a example for fstream as it did for the rest of the items listed for other items .
but , thats beside the point
here is what i didnt understand

indata>>line1;
	cout << " The ASCII value of your character is " << (char)line1 << endl;

i knew that part but what i couldnt figure is how to make the number display back on the line at the end
the answer back should read

"the ASCII value of your character "h" is "104"
thats where my problem lies


2- for the 2nd part i wasnt sure if i had to break it up in to to seperate functions or not

3-next i was thinking once i figured out if i need two functions t how was i going to get the 2nd line of the" data.txt" files only to read the 46 and 54 .
am i going to need a ignore statement ?


4 - where the variables i used bad ? i see you changed them for the second part of the program.

5- why did you remove parts of my header

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

i thought i need a string to do the math part of this program

basically i need help in recognizing what commands i need to use to get it done , but i wasnt sure what where the right operators to get it all done ...next time ill ask the questions alittle better.
i wanted to see some work it out so i could see where my errors where and so i could ask questions based of his knowledge
,but im not always sure how to ask the correct questions im seeking.

now that i see the code i see a few place that now make sense , but i have more now to ask


o and i already have a house i dont live with my parents. but if you want you can come help pay some of my mortgage note feel free to help donate

let me start by telling you that i was solving it more for myself than you, i was not very sure about the ifstream/ofstream fns so i solved it and then posted the soln also, as a result i didnt really bother too much about solving the exact qs.. now i can ans the qs..

[TEX]1>"the ASCII value of your character "h" is "104"[/TEX]

for this you simply need to typecase line1 variable to int before giving it to cout

[TEX]2>for the 2nd part i wasnt sure if i had to break it up in to to seperate functions or not[/TEX]

upto you to decide

[TEX]3>next i was thinking once i figured out if i need two functions t how was i going to get the 2nd line of the" data.txt" files only to read the 46 and 54 .
am i going to need a ignore statement ?[/TEX]

not sure what ur saying here. in my code above i'm able to read the two lines and perform the calculations

[TEX]4>where the variables i used bad ? i see you changed them for the second part of the program[/TEX]

i just added a few more, sum and product were added to write into the new file and some char arrays for getline functions.

[TEX]5>why did you remove parts of my header[/TEX]

i was not sure y they were added, so i removed them to start with, thinking that i will include if the compiler cribs.. it didnt.

[TEX]Note: i'm sure there might be a better way to do it, you can dig deeper. i'm also learning :) .. keep trying[/TEX]

ok let me ask 2 questions

indata >> data.txt = line
               	cout << " The ASCII value of your character: " __ " is " ____" << endl;

1- i know i need a static cast on this statement , i have never done 1 so i wanted some one to show me the the syntax ...or an example of of it being used , because up till know i have only done "hello world" and "math average" programs . so there is my main problems i have never seen a program , or a skeleton snippet to see how its coded

2- my second question is how do you did you tell the program to read the 2nd line of the data.txt i never seen that coding structure before , again a skeleton snippet would be great

for 1> suppose you have a char and you have to typecast it to int

char a = 'r';
cout << (int)a; //cast char to int

for2> you can use 'getline' without the delim parameter, in that case it will read till it encounters '\n' or eof

char a[4];
char b[4];
indata.getline(a,4);
indata.getline(b,4); //reading next line

and i just tried, using the >> operator also takes you to the next line everytime.

char line1; 
	int line2a, line2b; 
	char val1[4],val2[4];	
	char delim = ' ';

	indata>>line1;
	cout << " The ASCII value of your character is " << (char)line1 << endl;
	;

	indata.getline(val1,4,delim);
	line2a = atoi(val1);

	indata.getline(val2,4,delim);
	line2b = atoi(val2);

You're making this way too complicated. Just do this:

char line1; 
int line2a, line2b;
indata >> line1;
indata >> line2a;
indata >> line2b;

The >> operator won't read the whole line. It'll read to the first white space, which is a space on line 2.

got it...

ok let explain this one more time
cause i dont think im explaining my question correctly

cout << " The ASCII value of your character: " __ " is " ____" << endl

all i want is for this to display both the letter "h" in the first "_" and then display its ascii value in the second "_" after the is so the statement reads when the program runs is

The ASCII value of your character "h" is "104"

the reason im so confused no one has told me yet how to around the word " is"

do i need a ignore statement

i have ran the code and all i get is either

The ASCII value of your character is "h"

or

The ASCII value of your character "h" "104"

now can anyone explain to me how i can
do do it the way i need to show it ?

If I'm reading this right, I'm guessing that your problem is that cout is treating the double quotes as marking the end of what you want to display and you can't tell it that you actually want to display the double quotes themselves? If that is a correct reading of the problem, there are at least two ways of dealing with it. One, display the quotes as a character rather than a string, as in:

cout << "These are double quotes: " << '"' << endl;

So that's a single quote (denoting a character) followed by the double quotes (the character to be displayed) followed by single quotes denoting the end of the character.

Another way is to use the escape character, which is the back slash \
That tells the cout statement not to try to interpret the double quotes as having their normal meaning (signifying the beginning or end of the text) but rather as the double quotes character with no special meaning, like this:

cout << "These are double quotes: \"" << endl;

So I have a back slash followed by double quotes (to be displayed) followed by another double quotes signifying the end of the statement to be displayed. In your program, the below does your printout and incorporates each style. Which style you use is simply personal preference.

char line1 = 'h';
int asciiVal = (int) line1;

cout << "The ASCII value of your character: \""
     << line1 << "\" is \"" << asciiVal << '"' << endl;

Hope I read your question right.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.