hi, I am new to programming and looking for some help, here is the portion of the assignment that is giving me headaches:

using one function, upload (i.e., input) the integer value and the color text, ignoring the word "Color:"; the function must only upload one data pair (i.e., the integer value and the color text) at a time, using pass by reference

the data.txt file is in this format:
9006045, Color: CYAN

I'm not sure how to a) get the data ignoring the (color:) portion of the dataset, and b) how pass by reference works? Thanks for any assistance.

Pass by reference allows one function to change the value of a variable that is declared in another function. For example, put the following into your compiler, compile and run it. Then you will see how pass by reference works.

More in-depts explaination is here.

#include <iostream>

void foo(int& x)
{
   x = 10;
}

int main()
{
   int color = 0;
   foo(color);
   std::cout << color << '\n';
}

well, because you didn't post any code and it sounds as if you already know how to retrieve lines from a file, I'm going to assume you are storing them in a string variable. So now you just need to break the string up correct?
I suggest you start by taking a look at substr:
http://www.cplusplus.com/reference/string/string/substr/

and find_first_of:
http://www.cplusplus.com/reference/string/string/find_first_of/

from the looks of it the comma and colon locations can be very helpful for stripping out the desired information.

passing by reference means you are passing the address of the variable, not just a copy of its value. So if you pass a variable to a function by reference then modify the variable within that function, the changes will persist even after you return from it.

You may be able to find more help by asking more specific questions and posting code you have so far

Thanks for the help.

here is what I was thinking:

  void getData (int& integerValue, string& color)
     {

             //stream name fin

         fin >> integerValue >> color;

         //while loop to check for end of data    
            while (!fin.eof())
               {
               //get the rest of the data
                 fin >> integerValue >> color;

               } 

       }       

after reading a little abut strings, I was thinking I can insert a check for the different colors, something like:

              str_t colorData;
              colorData=str.find_first_of("CYAN", "MAGENTA", "YELLOW", "BLUE");

since those are the only color possibilities in my data. Is there a better way?

Edited 3 Years Ago by Dani: Formatting fixed

find_first_of() doesn't work like that. There are several ways to do it, here is just one of them.

string colors[4] = {"CYAN", "MAGENTA", "YELLOW", "BLUE"};

<snip>
bool found = false;
for(int i = 0; i < 4; i++)
{
   if( colorData == colors[i] )
   {
      found = true;
      break;
   }
}

Edited 6 Years Ago by Ancient Dragon: n/a

ok, so that will retrieve a line of the data and break it up into an integer and a string. Now if I use a while.!eof loop, pass by reference will continue to pass each value from the data file to the variables? How can I output them?

This article has been dead for over six months. Start a new discussion instead.