I am new to C++ and am writing a program that will call other functions. I keep getting an error for a undeclared identifier for "vowel" in this section of code. Can anyone help?

#include <iostream>
#include <iomanip>
using namespace std;

void generate_random_vowel(char &vowel); 

int main ()
{
    char vowel;
    //call test function
    generate_random_vowel(vowel);
}

void generate_random_vowel (char &vowel) //function for generate_random_vowel. Outputs a random vowel
{
    srand(22); //number to seed the rand 
     //random numbers 1 - 5, there are 6 vowels (including sometimes Y). Start with case 0.
    switch (vowel) //will pull a case at random for a random vowel. Written in this fashion for a direct cout and to save space
    {
        case0:std::cout<<'A'; break; //make sure break is included
        case1:std::cout<<'E'; break; //make sure break is included
        case2:std::cout<<'I'; break; //make sure break is included
        case3:std::cout<<'O'; break; //make sure break is included
        case4:std::cout<<'U'; break; //make sure break is included     
        case5:std::cout<<'Y'; break; //make sure break is included
        default:std::cout<<"Sorry, try again"; break; //error with the random pull, advising the user to try again 
    }
}

Edited 3 Years Ago by Reverend Jim: Fixed formatting

I am new to C++ and am writing a program that will call other functions. I keep getting an error for a undeclared identifier for "vowel" in this section of code. Can anyone help?

I get different errors with the code you posted (which would have formatting preserved if you used code tags): case statements have a space after the keyword case .

With random numbers, you seed the generator only once, typically by srand(time(0)); . To obtain a random number call rand() .

I don't suppose this is anything close to what you might be attempting:

#include <iostream>
#include <ctime>
using namespace std;

void generate_random_vowel(char &vowel);

int main ()
{
   char vowel;
   srand(time(0));
   vowel = rand() % 6; // get value 0-5
   //call test function
   generate_random_vowel(vowel);
}

void generate_random_vowel (char &vowel) //function for generate_random_vowel. Outputs a random vowel
{
//random numbers 1 - 5, there are 6 vowels (including sometimes Y). Start with case 0.
   switch ( vowel ) //will pull a case at random for a random vowel. Written in this fashion for a direct cout and to save space
   {
   case 0:std::cout<<'A'; break; //make sure break is included
   case 1:std::cout<<'E'; break; //make sure break is included
   case 2:std::cout<<'I'; break; //make sure break is included
   case 3:std::cout<<'O'; break; //make sure break is included
   case 4:std::cout<<'U'; break; //make sure break is included
   case 5:std::cout<<'Y'; break; //make sure break is included
   default:std::cout<<"Sorry, try again"; break; //error with the random pull, advising the user to try again
   }
}

BTW, this is the C forum.

You are passing vowel incorrectly. The & symbol is used in main to pass vowel by reference, then the * pointer is used in the actual function

void generate_random_vowel (char* vowel)
{
    *vowel = 'a';
}

int main()
{
   char vowel;
   generate_random_vowel( &vowel );
}

Thanks for the help....I am trying to generate a random vowel into the variable sent to the function as an argument, to change the value of that variable. As part of the assignment I am not allowed to add #include <ctime>.

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