Thread: how do we reverse ??

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    4

    how do we reverse ??

    hi people
    i am havinfg difficulties with some aspects of my program. the latter basically should open a file, the user should then input three numbers and these should be written/inputted in the file.
    The program then reads and prints these three numbers to screen. Then, the program opens another file and asks for your mother's maiden name. the latter is printed to screen and this is where the problem arises: we are then required to print the maiden name to screen in reverse order....and i donot have a clue of how to do it. How do we write what we inputted in reverse order ??? Also, i cannot make the program read and print all 3 numbers that were inputted before. it just print the first number.

    here is my code:

    #include <fstream.h>
    #include <iostream.h>
    #include <stdio.h>

    int main()
    { char quit;
    while (quit != 'q')

    {
    char numbers[10]; // ???
    char name[70];
    float a, b , c;




    ofstream a_file("peter.txt"); //Creates an instance of ofstream, and opens peter.txt


    cout<<"Please enter three numbers.\n";
    cout<<"First number:\n";
    cin>>a;
    cout<<"Second number:\n";
    cin>>b;
    cout<<"Third number\n";
    cin>>c;
    a_file<<a<<", "<<b<<", "<<c; //Outputs to peter.txt through a_file

    a_file.close(); //Closes up the file


    ifstream b_file("peter.txt"); //Opens for reading the file

    b_file>>numbers; //--not working !!!--it only reads the first number and not all of them.

    cout<<numbers; //outputs the numbers that have been inputted --not working !!! only first number is printed....

    cout<<"\n";

    b_file.close(); //This closes the file


    ofstream c_file("therese.txt"); //Creates an instance of ofstream, and opens therese.txt
    cout<<"Write your mother's maiden name.\n";
    gets(name);
    c_file<<name; //Outputs to therese.txt through c_file

    /* i could close the file here, and reopen it for it to be read just like the one before.
    But i get the same problem, it prints only the first word and not the second one ...so this is easier and gets rid of this problem...*/


    cout<<"You mother's maiden name is "<<name; //outputs the name that have been inputted.

    cout<<"\n";

    c_file.close(); //This closes the file

    */ Now, how do I make the program print what i've writen in
    reverse order ???? */

    cout<<"press q to quit\n";
    cin>>quit;
    }
    return 0;
    }

  2. #2
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    A) When your program tries to read the first 3 numbers, it is catching the space and ending the input because it thinks the space is the end. You could use b_file.getline(numbers, "\n"). Look up the function for details.

    B)To reverse the name, create a temp array and start from the end of the original array and put that char into the beginning of the temp array.

  3. #3
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    there is a strrev() in string.h but it reverses the orig string
    -

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with my reverse function and its output!
    By Matus in forum C Programming
    Replies: 4
    Last Post: 04-29-2008, 08:33 PM
  2. a reverse function
    By AngKar in forum C Programming
    Replies: 20
    Last Post: 04-27-2006, 10:35 PM
  3. Using reverse iterators in algorithms
    By 0rion in forum C++ Programming
    Replies: 1
    Last Post: 02-27-2006, 03:19 AM
  4. gethostbyaddr() reverse lookups failing (???)
    By Uncle Rico in forum C Programming
    Replies: 9
    Last Post: 08-19-2005, 09:22 AM
  5. Replies: 7
    Last Post: 03-18-2003, 03:32 PM