check my code please

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • kittykat

    check my code please

    Hi,
    could someone please check my code? its asking the user to enter 3
    letters, and check to see if these letters exist in the text file. i know
    ive done something wrong. can someone fix my code please??

    #include <fstream>
    #include <string>
    #include<iomani p>
    #include <iostream>
    #include<istrea m>
    #include <cstdlib>
    using namespace std;

    int main()
    {
    char a, b, c;
    ifstream myFile("data.tx t");

    cout << "Enter the pattern you are searching for: ";
    cin.getline >> a & b & c;

    cout << "Program will now search for: " << a << b << c << endl;

    return 0;

    }

  • Howard

    #2
    Re: check my code please


    "kittykat" <f_arikat@nospa m.hotmail.com> wrote in message
    news:ad0d532855 cb9390a4245bdc9 949c424@localho st.talkaboutpro gramming.com...[color=blue]
    > Hi,
    > could someone please check my code? its asking the user to enter 3
    > letters, and check to see if these letters exist in the text file. i know
    > ive done something wrong. can someone fix my code please??
    >
    > #include <fstream>
    > #include <string>
    > #include<iomani p>
    > #include <iostream>
    > #include<istrea m>
    > #include <cstdlib>
    > using namespace std;
    >
    > int main()
    > {
    > char a, b, c;
    > ifstream myFile("data.tx t");
    >
    > cout << "Enter the pattern you are searching for: ";
    > cin.getline >> a & b & c;
    >[/color]

    What in the world does that do? Isn't the format for getline:
    getline(cin,str ); ?
    [color=blue]
    > cout << "Program will now search for: " << a << b << c << endl;
    >[/color]

    Aren't you supposed to DO the search here???
    [color=blue]
    > return 0;
    >
    > }
    >[/color]

    Is this some kind of joke? Trying to trick someone into doing your work for
    you? Surely you don't expect us to think you don't see there isn't even one
    line of code that searches in the text file for a match! At least TRY to do
    your own work first. This is not an effort, it's just silly.

    -Howard




    Comment

    • Yoon Soo

      #3
      Re: check my code please

      kittykat wrote:
      [color=blue]
      > Hi,
      > could someone please check my code? its asking the user to enter 3
      > letters, and check to see if these letters exist in the text file. i know
      > ive done something wrong. can someone fix my code please??
      >
      > #include <fstream>
      > #include <string>
      > #include<iomani p>
      > #include <iostream>
      > #include<istrea m>
      > #include <cstdlib>
      > using namespace std;
      >
      > int main()
      > {
      > char a, b, c;
      > ifstream myFile("data.tx t");
      >
      > cout << "Enter the pattern you are searching for: ";
      > cin.getline >> a & b & c;
      >
      > cout << "Program will now search for: " << a << b << c << endl;
      >
      > return 0;
      >
      > }
      >[/color]

      have a look at this for some algorithms in pattern matching:



      the naive algorithm should be easy enough to understand

      greets

      yoon

      Comment

      • kittykat

        #4
        Re: check my code please

        Thank you so much Yoon Soo!!! :)

        Comment

        • kittykat

          #5
          Re: check my code please

          Hi Howard,
          this is my first week of trying anything in C++.

          "Is this some kind of joke? Trying to trick someone into doing your work
          for you?"

          not a joke at all. i just wanted some help. my experience is very limited
          and i am trying my best here.

          "Isn't the format for getline: getline(cin,str );"

          so in my case, would i write
          getline(cin, a,b,c);
          would that work?


          Comment

          • E. Robert Tisdale

            #6
            Re: check my code please

            kittykat wrote:
            [color=blue]
            > Could someone please check my code?
            > It's asking the user to enter 3 letters
            > and check to see if these letters exist in the text file.
            > I know [that] I've done something wrong.
            > Can someone fix my code please?[/color]
            [color=blue]
            > expand kat.cc[/color]
            #include <fstream>
            #include <string>
            #include <iomanip>
            #include <iostream>
            #include <istream>
            #include <cstdlib>
            using namespace std;

            int main(int argc, char* argv[]) {
            char a, b, c;
            ifstream myFile("data.tx t");

            std::cout << "Enter the pattern you are searching for: ";
            std::cin >> a >> b >> c;

            std::cout << "Program will now search for: "
            << a << b << c << std::endl;

            return EXIT_SUCCESS;
            }
            [color=blue]
            > g++ -Wall -ansi -pedantic -o kat kat.cc
            > ./kat[/color]
            Enter the pattern you are searching for: a b c
            Program will now search for: abc

            Now write some code to search "data.txt" and post it here.

            Comment

            • Karl Heinz Buchegger

              #7
              Re: check my code please

              kittykat wrote:[color=blue]
              >
              > Hi Howard,
              > this is my first week of trying anything in C++.[/color]

              If this is true (and I don't doubt it)
              then the assignment is much to hard for you (as for
              anybody else with just one week of experience).
              [color=blue]
              >
              > "Is this some kind of joke? Trying to trick someone into doing your work
              > for you?"
              >
              > not a joke at all. i just wanted some help. my experience is very limited
              > and i am trying my best here.
              >
              > "Isn't the format for getline: getline(cin,str );"
              >
              > so in my case, would i write
              > getline(cin, a,b,c);
              > would that work?[/color]

              No.
              Which books are you learning from?

              Read 3 characters: When we are allowed to ignore error checking
              for the moment, it is written:

              char a, b, c;

              cin >> a >> b >> c;


              Your version cannot work. Mostly because as you say for yourself:
              [color=blue]
              > "Isn't the format for getline: getline(cin,str );"[/color]

              getline takes 2 arguments. You try to feed it
              [color=blue]
              > getline(cin, a,b,c);[/color]

              4 arguments. 2 - 4. That won't work. Also: getline expects
              a *string* variable. You don't have one. You have 3 char
              variables.
              char - stores a single character
              string - stores a sequence of characters.

              --
              Karl Heinz Buchegger
              kbuchegg@gascad .at

              Comment

              • Howard

                #8
                Re: check my code please


                "kittykat" <f_arikat@nospa m.hotmail.com> wrote in message
                news:92ff884be7 2835b3efbca44e8 000823f@localho st.talkaboutpro gramming.com...[color=blue]
                > Hi Howard,
                > this is my first week of trying anything in C++.
                >
                > "Is this some kind of joke? Trying to trick someone into doing your work
                > for you?"
                >
                > not a joke at all. i just wanted some help. my experience is very limited
                > and i am trying my best here.
                >[/color]

                Ok, sorry if I was harsh. But we see a lot of students here simply asking
                us to do their work for them. And your post only said "something is wrong",
                when it looks pretty obvious to me that the main problem is that there's no
                code there at all that does what you're saying the program should do (which
                is to match up the pattern with the text in the file). See what I mean? If
                the problem you were having was a compiler error on the call to getline,
                then you need to tell us, so that we can concentrate on fixing that.

                -Howard


                Comment

                • kittykat

                  #9
                  Re: check my code please

                  thanx so much. I am currently working on this, and will post it as soon as
                  i have finished. I have taken some advice from my previous "Pattern
                  Matching" post, and have decided to break the problem down into tiny bits.
                  once i have something completed, i will post it and see what you think.

                  Thanx so much once again for everyones help. i really appreciate it!

                  Comment

                  Working...