Need Some Help

This is a discussion on Need Some Help within the C++ Programming forums, part of the General Programming Boards category; I am in a C++ class and one of the assignments is going to be below. I have read through ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    1

    Question Need Some Help

    I am in a C++ class and one of the assignments is going to be below. I have read through the chapter and gone over the lecture and I am still having problems. If someone could explain this to me it would be greatly appreciated. Ok here is the assignment.

    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)

    Remember to comment your code to explain what you are doing, and remember to close both the input and output file when you are done manipulating them.



    I'm not asking for the answer. Would just like someone to explain it to me so that I can do it.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Your assignment already is a step-by-step explanation of the problem. 1. Use fstream to read in your text file (presumably into variables you've made); 2. Make more variables to be the sum, product, and ASCII value; 3. Write out the three sentences given to you in the problem, using the values of the variables to fill in the blanks; 4. Close both the input and output files.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And which part(s) of the problem have you already done, and which have you got a problem with? If the answer is "none" and "all", then you probably need to go over your lecture notes or read the chapter(s) in the book you should know by now.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Nor
    Nor is offline
    h ֆhr s Nor's Avatar
    Join Date
    Nov 2001
    Posts
    299
    I'm going to try not to do your homework for you and at the same time help you out.
    do some homework on fstream/iostream/ and common string functions like atof()

    all file input/output will be in TEXT format. not Binary
    Code:
    input file.
    ... abcdefghijklmnopqrstuvwxyz
    ... 10
    ... 20
    ... 30
    include <fstream>
    include <iostream>
    int main(){
        char* p;
        float  number_1;    // i'm using float to make you do some research.
        float  number_2;
        float  number_3;
        float  number_4;
        std::fstream inputfile;
        file.open("myfile.txt", std::ios::in);
        //refer to other parts of this site for error checking.
    
    //first lets get some room to work.... by that i mean storage space.
        p = new char[100];
    //error checking again
        memset(p,0,100);    //clear the work space;
        inputfile.read( p );     //<< more homework here.
    
    //now your buffer  p  should contain your entire text file. 
    //TIP 1: the lines in the file will be sepraerated by '\n' and not \0
    
    //now lets read the first line.
    magical_function_you_need_to_write_to_find_where_newline_is( p );
    //Image we have another file called outputfile
    outputfile.write(p, magical_function_return_value());
    file.getline(p, 100);
    //please note the above function.
    //anyway enought TIPS 2:
    
    //Now we just got the numbers.
    number_1 = atof(p);
    
    file.getline(p, 100);
    number_2 = atof(p);
    
    file.getline(p, 100);
    number_3 = atof(p);
    
    if( (number_1 + number_2) != number_3)
        exit(1)//10+20 != 30 then something is wrong. but i am using float's. hehehe
    
    std::cout "The sum of \"" << number_1 << "\" and \"" << number_2 " = \"" << number_3;
    hope this helps. but as they said eariler the porblem is perty much self explanitory(forgive my spelling.)
    basicly the excersize is to get you familer with file input/output and get you using the std libary. after you learn how to get JUST what you want out of a file you can start learning the printf and _snprintf() functions.
    Try to help all less knowledgeable than yourself, within
    the limits provided by time, complexity and tolerance.
    - Nor

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,666
    What horrible mixture of C++ and C did you learn and are spreading around? And why bother with filestreams? that's nowhere near the theoretical foundation OP would need to fill in the blanks for his program here ...

Popular pages Recent additions subscribe to a feed

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21