Thread: help me out!

  1. #1
    Registered User newbie_grg's Avatar
    Join Date
    Jul 2002
    Posts
    77

    help me out!

    heres the code
    #include<iostream.h>
    #include<vector>
    #include<string>
    #include<conio>

    using namespace std;
    struct pair{
    char* name;
    double val;
    };
    vector<pair>pairs;
    double &value(const char* &s){
    for(int i=0;i<pairs.size();i++){
    if(s==pairs[i].name){return pairs[i].val;}

    else {pair p={s,0};
    pairs.push_back(p);
    } }
    return pairs[pairs.size()-1].val;
    }
    void main()
    {
    char* buf;
    while(cin>>buf) {value(buf)++;}
    for(vector<pair>::const_iterator p=pairs.begin();p!=pairs.end();++p)
    {cout<<p->name<<":"<<p->val<<"\n";
    }getch();}

    NOW THE PROBLEM IS THAT I GET THE ERROR WRITTEN IN PREVIOUS MESSAGE "DEEEP #$%&" THIS CODE IS BEEN TAKEN FROM " THE C++ PROGRAMMING LANGUAGE"BY BJARNE STROUSTRUP. IS MY COMPILER OUTDATED???HELP ME
    "If knowledge can create problems, it is not through ignorance that we can solve them. "
    -Isaac Asimov(1920-1992)

  2. #2
    Registered User
    Join Date
    Jul 2002
    Posts
    11
    apparently there is something wrong with

    #include<conio>

    I am guessing the compiler can't find it or it doesn't exist. search for it on the web if it doesn't exist on ur harddrive
    Last edited by RPG++; 07-30-2002 at 08:10 AM.
    www.solidgone.com

  3. #3
    Registered User newbie_grg's Avatar
    Join Date
    Jul 2002
    Posts
    77

    no wrong!

    RPG++ said "apparently there is something wrong with

    #include<conio> "
    i have checked the source code and compiled it.yet there isnt anything wrong with <conio>
    "If knowledge can create problems, it is not through ignorance that we can solve them. "
    -Isaac Asimov(1920-1992)

  4. #4
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    >IS MY COMPILER OUTDATED???

    What compiler are you using? Don't yell.

  5. #5
    try taking the .h off of iostream. The using namespace std only works for the includes without .h

  6. #6
    Registered User
    Join Date
    Jul 2002
    Posts
    11
    no clue. i was using Dev C++ even though i have Visual C++ but was too lazy to start it up.
    www.solidgone.com

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >THIS CODE IS BEEN TAKEN FROM " THE C++ PROGRAMMING LANGUAGE"BY BJARNE STROUSTRUP.
    No it hasn't. The creator of C++ would never code in such an incorrect and haphazard manner. You mean you read something in the book and tried to code it on your own. There are a great number of problems with this code, I'll try to get them all:

    >#include<iostream.h>
    This is the old header, if your compiler supports namespaces then you should be using <iostream>.

    >#include<conio>
    This should be <conio.h>.

    >struct pair{
    The identifier pair is already used by the STL, this should cause an abiguity warning.

    >char* name;
    Use the string class instead, it is far safer than manually dealing with memory for char *'s.

    >double val;
    If this is supposed to be a counter for duplicate items then it should be int.

    >double &value(const char* &s){
    Once again you'd best use the string class to avoid pointer problems.

    >if(s==pairs[i].name){return pairs[i].val;}
    You can't compare char *'s with the equality operator, either use strcmp in string.h or use the string class, which has the functionality you want.

    >void main()
    No, main returns an int and nothing else.

    >while(cin>>buf)
    You're assigning to memory that you don't own, buf is just a pointer and it can't hold a string until you assign enough memory for it to do so. The result is undefined behavior, you don't want this.

    >value(buf)++;
    I know this feature may look attractive, but it does nothing but make the code more confusing in this case.

    >getch();
    getch really isn't needed here since cin.get() does the same thing when used in this context.

    Here's a quick modification of the program, it's still not great, but at least it works and doesn't exhibit undefined behavior:
    Code:
    #include<iostream>
    #include<vector>
    #include<string>
    
    using namespace std;
     
    struct my_pair
    {
      string name;
      int val;
    };
    
    vector<my_pair> pairs;
    
    void value(string &s)
    {
      for(int i=0;i<pairs.size();i++){
        if(s==pairs[i].name){
          // Already exists, increment the count.
          pairs[i].val++;
        }
      }
      // Not already in the vector, add it.
      my_pair p;
      p.name = s;
      p.val  = 0;
      pairs.push_back(p);
    }
     
    int main()
    {
      string buf;
      while(cin>>buf) {
        value(buf);
      }
      vector<my_pair>::const_iterator p = pairs.begin();
      while(p != pairs.end())
      {
        cout<<p->name<<":"<<p->val<<"\n";
        p++;
      }
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed