I need help with my basic program
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: I need help with my basic program

  1. #1
    Join Date
    Nov 2012
    Posts
    4

    I need help with my basic program

    My job is to read in a line of data from an input file, remove all white space and punctuation, then check to see whether or not it is a palindrome. A palindrome is a word or phrase that is the same backwards as it is forward like RACECAR.
    The input file is an arbitrary length long including no input. I need to read in a line of data at a time then process it until there is no more data to process.
    The output should look something like this.

    Input Value: Sit on a potato Pan otis!!!
    Is it a palindrome? Yes

    Input Value: race car
    Is it a palindrome? Yes

    Input Value: wooHaaaaa
    Is it a palindrome? No

    I am very new to Programming so anything that you smarter people can do to help me would be greatly appreciated!
    This is what I have right now:

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

    using namespace std;

    ifstream infile;
    ofstream outfile;

    int main()
    {
    ifstream inData;
    ofstream outData;
    while(!infile.eof())
    {
    char sentence[90];

    inData.open("InPalindrome.txt");
    outData.open("OutPalindrome.txt");

    if (!inData)
    {
    cout << "Can't open input file successfully";
    getchar ();
    return 1;
    }
    if (!outData)
    {
    cout << "Can't open output file successfully";
    getchar ();
    return 2;
    }

    outData << "Input Value :"<<endl;

    inData >> sentence;

    int x = strlen(sentence)-1;



    for(int i = 0; i <= x; i++)
    {
    if (sentence[i] == sentence[x-i])
    {
    continue;
    }
    else
    {
    outData<<"Sentence is a palidrome"<<endl;
    getchar ();
    return 0;
    }
    }


    outData<< "Sentence is a Palidrome"<<endl;


    }
    inData.close();
    outData.close();
    getchar ();
    return 0;
    }


    My debug is successful but it just doesn't work how I would like it too
    Thanks again!

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,047

    Re: I need help with my basic program

    "My debug is successful but it just doesn't work how I would like it too"

    That's a contradictory statement. What doesn't work? Off the top of my head I see your loop checks the entire string, when it should stop in the middle.

  3. #3
    Join Date
    Nov 2012
    Posts
    4

    Re: I need help with my basic program

    I was meaning to say that when I build the solution it is successful but I really dont know how to get the outcome that I need

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,047

    Re: I need help with my basic program

    It would really help if you said how its behavior differs from what you want

  5. #5
    Join Date
    Apr 1999
    Posts
    27,422

    Re: I need help with my basic program

    Quote Originally Posted by Rookie24 View Post
    I was meaning to say that when I build the solution it is successful
    OK, you should understand something really basic.

    Building a C++ program successfully only means that your code is syntactically correct, and that the linker finds all of the external functions your application calls. That's it, nothing else.

    Nowhere does a successful build of an application ensures that the application will work correctly. If I was told to write a program to add two numbers, but instead the application subtracts the two numbers, does the application compile and build successfully? Yes. However, does the program work correctly (adds two numbers). No.

    So now what you should be doing is to debug your program. Debugging a program that you wrote is part and parcel of learning how to write programs. It isn't just about writing code, running the code, and then posting on CodeGuru or some other forum that your program doesn't work. You have to take the program you wrote and identify by debugging where it goes wrong. No one writes flawless, bug-free programs every single time, and debugging is the mechanism used to figure out what is wrong with the program.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; November 6th, 2012 at 12:08 PM.

  6. #6
    Join Date
    Nov 2012
    Posts
    4

    Thumbs up Re: I need help with my basic program

    My program right now is just outputting to the screen the first 3 lines of my input file when I run the program. I need it to read in all of the lines, determine if the phrases are a palindrome or not and then it needs to produce an output report both to the screen and to an output file. The output should look like this:

    Input Value: race car
    Is it a palindrome? Yes

    Input Value: mom
    Is it a palindrome? Yes

    Input Value: pop
    Is it a palindrome? Yes

    Input Value: Sit on a potato Pan otis!!!
    Is it a palindrome? Yes

    Input Value: program
    Is it a palindrome? no

    Input Value: wooHaaaaa
    Is it a palindrome? No



    However, the output to the screen that I am getting is:

    race car
    mom
    pop


    Thanks again for your help, I really appreciate it

  7. #7
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,047

    Re: I need help with my basic program

    This really is the time to learn to use the debugger. It'll show you everything your program is doing and why it's doing it. It's absolutely indispensable, and it's what every one of us here would do to find out what your program is doing. You really can't write even simple programs without it.

  8. #8
    Join Date
    Apr 1999
    Posts
    27,422

    Re: I need help with my basic program

    Quote Originally Posted by Rookie24 View Post
    My program right now is just outputting to the screen the first 3 lines of my input file when I run the program. I need it to read in all of the lines, determine if the phrases are a palindrome or not and then it needs to produce an output report both to the screen and to an output file. The output should look like this:
    Again, writing and debugging programs is something you must learn to do. Just saying that "I wrote a program and it doesn't work. Make it work for me" isn't going to get you far here.

    I know you're a beginner, but you have to understand that it isn't "wrong" to have a program that doesn't work correctly initially. That's the nature of programming -- all of us have written and still write programs that initially do not behave correctly. That is where the other part comes in, and that is debugging. This is where you take a program that doesn't behave correctly, and correct it.

    You wrote the program therefore you had something in mind when you wrote the program. So take the debugger, step through your program, and see where the program deviates from the original plan. Again, you wrote the program -- if you wrote the program, you must know what you're doing every single step of the way, else you could never have written it. Now if the program doesn't behave correctly, you know exactly what parts of the code are supposed to do "x", and if it doesn't do "x", you fix it by debugging it.

    Also, you should use code tags when posting code so that the code is formatted and readable.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; November 6th, 2012 at 04:50 PM.

  9. #9
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,047

    Re: I need help with my basic program

    One other thing, since we don't have your input file, none of us can reproduce what you're seeing without guessing.

  10. #10
    Join Date
    Nov 2012
    Posts
    4

    Re: I need help with my basic program

    This is all that my input file says:

    race car
    mom
    pop
    sit on a potato pan Otis
    Madam in Eden I'm Adam
    Potatoe
    Go hang a salami I'm a lasgna hog
    Last edited by Rookie24; November 6th, 2012 at 06:46 PM.

  11. #11
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,047

    Re: I need help with my basic program

    The debugger very quickly shows that you're reading and testing one word at a time, not one line.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center