Announcement

Collapse
No announcement yet.

Can you help me, to solve this exercise in c++?

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

    Can you help me, to solve this exercise in c++?

    hi, you all. I am a beginner c++ programmer.
    My teacher, has given me, this exercise in c++ language.
    Read from a file the names, print them and their position. So, print, how many times, a name is presents into a file.
    file input: names
    Carlo
    Antonio
    Marco
    Antonio
    Carlo
    Carlo
    Marco
    output program:
    list names:
    Carlo 0
    Antonio 1
    Marco 2
    Antonio 3
    Carlo 4
    Carlo 5
    Marco 6

    Carlo is present: 3 times
    Antonio is present: 2 times
    Marco is present: 2 times
    this is my code:
    Code:
    #include<iostream>
    #include<fstream>
    #include<string.h>
    
    using namespace std;
    
    int main()
    {
      ifstream file;
      ifstream f;
      char name[1024],name_new[1024];
      int i,pos=0,occ=0;
      file.open("names",ios::in);
      while(!file.eof())
      {
       file>>name;
       if(name[1024]!='\n')
       {
         cout<<name<<"\t"<<pos<<endl;
         pos++;  
       }  
      }  
      cout<<endl;
      file.close();
      file.open("names",ios::in);
      f.open("names",ios::in);
      for(i=0;i<pos;i++)
      {
       file>>name;
       for(i=0;i<pos;i++)
       {
         f>>name_new;
         if(strcmp(name,name_new)==0)
         {
          occ++;
         }
       }
       cout<<name<<"\t"<<occ<<endl;
      }
      file.close();
      f.close();
      return 0;
    }
    but it do not run very well.....

    #2
    Re: Can you help me, to solve this exercise in c++?

    Generally forums don't help people with their homework.

    You will learn more by reading and sorting it out yourself, rather than having someone give you the answer.
    "A problem well stated is a problem half solved." --Charles F. Kettering
    "Sometimes the questions are complicated and the answers are simple."--Dr. Seuss

    Comment


      #3
      Re: Can you help me, to solve this exercise in c++?

      Ok, I am sorry....

      Comment


        #4
        Re: Can you help me, to solve this exercise in c++?

        Excellent advice, arochester!

        When I was attending college I worked my way through. During my last two undergraduate years I was as an analytical chemist at Bradford labs, which was a subsidiary of Calgon Corp. I analyzed just about anything the engineers brought in the door ... jars of water, soil, mud, clumps of bubbly stuff that gave off explosive gas (H2), slime, goo, duck feathers (natural or processed?), blood (human or not?), bacteria (genus/species), etc... (I didn't do the regular four years. I only took 12 hrs per semester so my Junior and Senior years took 3 years and two summers. My graduate work took 3 years, but I was on scholarship during that time.

        During my Senior year I was instructor in the Analytical Chem lab. One day, while I was back in the lab, I heard the front door open and a familiar voice speak to the receptionist. She came back into the lab and said a young man had given her this test tube full of stuff and he wanted us to analyze it for him. I told her to accept it, have him fill out the usual contract and sign it. Sure enough, it was the mid-term analysis tube I had given this guy earlier that morning in the Analytical lab. I already knew what was in it. I filled out the form stating the contents of the tube, and its percentages. I watched him doing his lab work on the mid-term the next day. He put on quite a show pretending to be analyzing the contents of his tube, but I still had his real tube in my lab coat, along with his signed contract. That afternoon he picked up the report, paid $50 (1965 money!), and the next morning presented "his" analysis to me at the end of the lab period. I took it and told him he earned an immediate "F" for the class and expulsion from it. "Why?", he asked. I produced his signed contract and the REAL tube and said "that's why!".
        He was also expelled from the college.

        His programming class instructor probably has coaching labs set up where this guy could go for help if he needed it.
        "A nation that is afraid to let its people judge the truth and falsehood in an open market is a nation that is afraid of its people.”
        – John F. Kennedy, February 26, 1962.

        Comment


          #5
          Re: Can you help me, to solve this exercise in c++?

          hehe...

          thanks for the flash back to high school...i promise that if you figure out where your code is going astray then you will always remember it. if we (or i ) tell you then you will only forget mear moments later.

          Mark Your Solved Issues [SOLVED]
          (top of thread: thread tools)

          Comment


            #6
            Re: Can you help me, to solve this exercise in c++?

            Programming, like many trades (including in the extreme case the trade of pure mathematics) is an art, far more than a science, and as such is far more inductive and experimental than deductive. There is no single or right answer. It comes as much from contemplation and reflection as it does from forcing symbols upon a page. To use someone else's program is to deprive yourself of the process of creating your own art.
            An intellectual says a simple thing in a hard way. An artist says a hard thing in a simple way. Charles Bukowski

            Comment


              #7
              Re: Can you help me, to solve this exercise in c++?

              Luigi, Its been years since I did any serious c programming. My advice is to get a good IDE environment to help you learn C.

              You need to ask yourself what you want to accomplish here. Is it just to get a passing grade and then move on, or to really learn C.

              Everyone has their favorite compiler. Here is a good Integrated Development Environment (IDE) system, and its free. Called Anjuta , but its for Gnome.
              You didn't give us much information or what system your using.

              Using an IDE system can assist you in finding bugs, step through your code, breakpoints, and a ton more features.
              Boot Info Script

              Comment


                #8
                Re: Can you help me, to solve this exercise in c++?

                IMHO there is nothing wrong in asking for help/tips/tutoring as long as you are not asking for someone to do your homework for you.

                Here's a couple of quick pointers (after a quick skim through the code) that may help you move along:
                1. You are using the same 'i' variable in both your for loops, which means that the outer for loop is run only once as the 'i' grows inside the inner for loop
                2. Your 'occ' variable does not get reset (between names) and grows indefinitely.
                3. I'm not convinced your algorithm for finding out the name counts works as expected (for example, looks like it will process all the lines in the file even if a particular name has already been counted).

                Good luck with your studies.

                Comment


                  #9
                  Re: Can you help me, to solve this exercise in c++?

                  Originally posted by verndog
                  ...
                  My advice is to get a good IDE environment to help you learn C.
                  ....

                  Using an IDE system can assist you in finding bugs, step through your code, breakpoints, and a ton more features.
                  For C++ a good tool is Kate, combined with Kgdb. After you learn C++ then the best way to avoid reinventing the wheel is to use a good C++ API toolkit, like Qt4. The GUI RAD package to use with it is QtCreator. For backend purposes you can't go wrong with PostgreSQL 8.x, using pgAdmin3 as the admin tool.
                  "A nation that is afraid to let its people judge the truth and falsehood in an open market is a nation that is afraid of its people.”
                  – John F. Kennedy, February 26, 1962.

                  Comment


                    #10
                    Re: Can you help me, to solve this exercise in c++?

                    To use someone else's program is to deprive yourself of the process of creating your own art.
                    Couldn't agree more! That is how I always have viewed my code.... A magnificent work of art!!!

                    Comment


                      #11
                      Re: Can you help me, to solve this exercise in c++?

                      Kinda like what the world-renowned mathematician Paul Halmos said about (pure) mathematics: It is an art, very much like painting. It is not a deductive science but an endeavor of trial and error, experiments, and guessing. Was lucky enough to have Dr. Halmos for a Real Analysis course at Indiana University in 1972.
                      http://en.wikipedia.org/wiki/Paul_Halmos
                      An intellectual says a simple thing in a hard way. An artist says a hard thing in a simple way. Charles Bukowski

                      Comment


                        #12
                        Re: Can you help me, to solve this exercise in c++?

                        Originally posted by kubicle
                        IMHO there is nothing wrong in asking for help/tips/tutoring as long as you are not asking for someone to do your homework for you.
                        I agree. I think a lot more people would be writing code, if they could get over the initial hump and get some reward for their struggle. Frustration is a good thing right up until it makes a person quit. Unfortunately the OP didn't give us a clue how much time and effort was invested in the code he had, so it is hard to determine what he needed to hear.
                        FKA: tanderson

                        Comment


                          #13
                          Re: Can you help me, to solve this exercise in c++?

                          I think we're all just "Preaching To The Choir", since the OP hasn't returned in almost a week.
                          Boot Info Script

                          Comment

                          Working...
                          X