Inline function

This is a discussion on Inline function within the C++ Programming forums, part of the General Programming Boards category; I have been trying to learn how to use functions to make my code a lot less repeatitive, but I ...

  1. #1
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094

    Inline function

    I have been trying to learn how to use functions to make my code a lot less repeatitive, but I seem to have something wrong. I tried rereading the guides on the site, as well as looking into my C++ book, and couldn't find why this doesn't work. This is not the complete code, because that is 200 lines or so, so I dropped it down to the first 3 runes I parse for.

    If I leave the combo function in, I get nothing that comes after it in the program, I still get the "Reading from:" and the file path, but I don't get a count of how many of each I have. Any help is welcome .

    RuneParser.cpp
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    inline void combo(int first, int second)
    {
        while (first>=3)
        {
        first-3;
        second++;
        }
    }
    
    int main ( int argc , char ** argv )
    {
        std::string filename = "C:\\Documents and Settings\\Owner\\My Documents\\Parsing\\Runes.txt";
        if (argc >= 2) filename = argv[1];
        std::ifstream file( filename.c_str() , std::ios::in );
        if (!file.is_open())
            std::cout<<"Error opening file";
        else
            std::cout << "Reading from: " << filename << std::endl;
        std::string line;
        int el = 0;
        int eld = 0;
        int tir = 0;
    
        while (getline( file , line ))
    	{
    
            if (line.empty() || (line == "\r")) continue;
            int colon_pos = line.find(':');
            std::string rune = line.substr(colon_pos+2,line.size()-colon_pos-7);
            if (rune == "El")
                el++;
            else if (rune == "Eld")
                eld++;
            else if (rune == "Tir")
                tir++;
    	}
        combo(el,eld);
        combo(eld,tir);
    
        std::cout<< "El:" << el << endl;
        std::cout<< "Eld:" << eld << endl;
        std::cout<< "Tir:" << tir << endl;
        
    
    	std::cin.get();
    }
    Runes.txt
    Code:
    Number of Items : 39
    1: El Rune
    3: El Rune
    4: Tir Rune
    6: Tir Rune
    8: Eld Rune
    12: Tir Rune
    14: El Rune
    18: Tir Rune
    19: El Rune
    21: El Rune
    24: Tir Rune
    27: Eld Rune
    31: El Rune
    33: El Rune
    35: Eth Rune
    36: Tir Rune
    37: Tir Rune

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Code:
    inline void combo(int first, int second)
    {
        while (first>=3)
        {
        first-3;
        second++;
        }
    }
    Did you perhaps mean -= ?
    Also you are passing those parameters by value (aka copy) so they are not affecting the variables in your main. Which doesn't appear to be what you intended. Try passing them by reference

  3. #3
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    I did mean to do first= first-3; (is that what you meant with -=?)

    Thanks for the point in the right direction, I will go look up how to pass them by reference.

    Edit: Ahh, well that made a large difference, the first-3 was causing it to loop indefinately. Once I changed that and set the function to pass them as a reference it worked great, thank you. (int& first0) <~~ magic!
    Last edited by Wraithan; 10-22-2005 at 08:34 AM.

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    glad it worked.

    yes first -= 3 is shorthand for first = first - 3

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 07:54 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. Replies: 5
    Last Post: 02-08-2003, 07:42 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21