Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

C++ Please Halp

Name: Anonymous 2013-12-06 22:27

Hey, new to C++, and there's a little background to this question.  The teacher is kind of a dick, i.e., gives assignment that says write a program that does x, y, & z, using a bunch of very specific rules, i.e., use pass by value when pass by reference would be easier, or requiring you to use int parameters in a function, requiring you to use specific variable names, or even file names, etc... He gives 0 credit for assignments that aren't made to his exact specifications.  Needless to say I've gotten more than my fair share of zeros because I made a perfectly capable, running program that solved the problem, but I didn't name the variables what he wanted, or used pass by reference when he wanted by value, or something else stupid.  So long story short, I NEED to make sure this is EXACTLY like he wants.  My problem is, on this particular program, he says to use a void function, and the result should be returned to the main function.  I was under the impression that you can't do this without pass by reference... What am I missing?
Here is the program I wrote:

using namespace std;
void toUpper (char string[]);
int main ()
{
  //The stuff in the main function here is only so I can test my 'toUpper' function.
  char myArray [25];
  int result;
  cout << "Enter up to 24 characters" << endl;
  cin.getline (myArray, 25);
  cout << myArray << endl;
  toUpper (myArray);
  cout << myArray << endl;
return 0;
}

void toUpper(char string[])
{
    char ch;
    int index = 0;
    ch = string[index];
    while (ch != '\0')
    {
        if (ch >= 'a' && ch <= 'z') string[index] -= 32;
        index++;
        ch = string[index];
    }
    //There should be some kind of return statement here to return the answer, but that seems to contradict what I know about return statements...
}

And here is his EXACT specifications:

Write a function named toUpper.  This function is passed a null terminated string.  The function should convert each lowercase character in the string to its uppercase equivalent.  No other characters in the string should be changed.  The function declaration will be as follows:
void toUpper (car string []);
Note that the parameter is an IN/OUT parameter since it is used to pass data to the function and return the result.  The result should be returned.  DO NOT DISPLAY THE RESULT.

So am I just stupid? or is there just something stupid I am totally missing out on something stupid in return statements?

Name: Anonymous 2013-12-06 22:30

just realized I typo-ed on the declaration for his specifications, it should read:
void toUpper (char string[]);
I know that's obvious but if I didn't correct myself it would bug me...

Name: Anonymous 2013-12-07 1:19

>>1-2
char string[] is a char*, no? You have a pointer to the beginning of the block of characters; Change the contents of that block.

Don't change these.
Name: Email:
Entire Thread Thread List