How to make certain numbers equal certain strings

I need this understanding because I am doing a slot machine exercise, and for whatever random number between 0 and 3. How would I code it so that if a certain number was given, the outcome would be a string. for example, if I wanted an image called "image0.jpg" to be outputted, how would i code it so that whenever 1 is inputted, I get the picture outputted, or when "image1.jpg" is outputted whenever I hit number 1. here are the given instructions, followed by my code:
----------------------------------------------------------------------------
Function Requirements:
Signature of function (header)

Return value: Image - the Image object generated based on location and reel value passed in.

Parameters: (all by value)

Point - the lower left corner of image location for this reel
int - which reel image it is

Body of function

This function will construct and return a single Image object at the location passed in. The value passed in determines which image file it should use. You should have a minimum of 4 different reel images. I have provided 4 fruit images you can use, but you may use your own images instead.

You must use the following names for your images:

image0.jpg
image1.jpg
image2.jpg
image3.jpg
-------------------------------------------------------
Image out_image(Point pt, int picture)
{
if (picture == 0)
{
return "image0.jpg";
}
if (picture == 1)
{
return "image1.jpg";
}
if (picture == 2)
{
return "image2.jpg";
}
if (picture == 3)
{
return "image3.jpg";
}
}

Errors are on each line where return occurs;
error: conversion from ?const char*? to non-scalar type ?Image? requested
You are not returning the proper thing. It says you should return "a single Image object" at the given point. The picture is indexed by number, so the file you should open is "image0.jpg", "image1.jpg", etc.

You can use the standard string class for this:

1
2
3
4
5
6
  string filename;
  filename  = "image";
  filename += (char)(picture + '0');
  filename += ".jpg";

  ifstream imagefile( filename.c_str() );

You can also use a standard stringstream:

1
2
3
4
  ostringstream filename;
  filename << "image" << picture << ".jpg";

  ifstream imagefile( filename.str().c_str() );

(Notice in both examples that you have to pass a c-string to the ifstream constructor.)

Hope this helps.
Is there another way to do it? We haven't learned/been taught those string classes yet...
the other way is an old c trick:

1
2
3
4
5
6
7
8
9
10
11
12

char fileName[] = "Image*.jpg";

for (int nIndex = 1; nIndex < 4; nIndex++)
{
     fileName[5] = '0' + nIndex;
     // so you see the change...
     cout << fileName << endl;

     // do your fstream work.
} // end the for loop.


I don't know if you have learned c-style string or a character array like I present but it is the other way around the string class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
char* out_image(Point pt, int picture)
{
		if (picture == 0)
		{
				return "image0.jpg";
		}
		if (picture == 1)
		{
				return "image1.jpg";
		}
		if (picture == 2)
		{
				return "image2.jpg";
		}
		if (picture == 3)
		{
				return "image3.jpg";
		}
} 


One quick question why are you passing Point variable to out_image()
We know nothing about the "Image" type. From the OPs posts, I presume it contains positional information. (That is not uncommon.)

Azagaros's C trick is nice, but it does restrict you to ten files... this may not be an issue, however, so...
    +1
Topic archived. No new replies allowed.