Switch statements - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Switch statements

texmommyof4texmommyof4 Posts: 13Member
I have wrote a program that gives the user a choice of whether they want to initialize an array with random generated numbers or keyboard entry.(The user also specifies size). The random generated case worked at one point(I played with the code some more and now it gets stuck in loop). But the Case A for keyboard entry seems to get stuck in an infinite loop. What am I doing wrong here? Thank you in advance for your help.
[code]
void fillArray(short original[],char choice, short size)
{
srand(time(NULL));
short i,num;
for(i=0;i>original[i];
break;
//also have tried cin>>num;original[i]=num;tried a for loop as
//well.

case 'B':
case 'b': original[i]=rand();//puts random numbers into array
break;

}
}


}
[/code]

Comments

  • texmommyof4texmommyof4 Posts: 13Member
    : I have wrote a program that gives the user a choice of whether they want to initialize an array with random generated numbers or keyboard entry.(The user also specifies size). The random generated case worked at one point(I played with the code some more and now it gets stuck in loop[red](I fixed this)[/red]. But the Case A for keyboard entry seems to get stuck in an infinite loop. What am I doing wrong here? Thank you in advance for your help.
    : [code]
    : void fillArray(short original[],char choice, short size)
    : {
    : srand(time(NULL));
    : short i,num;
    : switch(choice)
    : {
    :
    : case 'A':
    : case 'a': cout<<endl<<"Enter "<<size<<" numbers";
    : cin>>original[i];
    : break;
    : //also have tried cin>>num;original[i]=num;tried a for loop as
    : //well.
    :
    : case 'B':
    : case 'b': for(i=0;i<size;i++)//fixed here
    original[i]=rand();//puts random numbers in
    : break;
    :
    : }
    : }
    :
    :
    : }
    : [/code]
    :

  • bilderbikkelbilderbikkel Posts: 754Member
    : [code]
    : [red]//void fillArray(short original[],char choice, short size)
    //Better (see www.codepedia.com/1/CppVector and www.codepedia.com/1/CppConst
    void fillArray(std::vector original,const char& choice, const short& size)[/red]
    : void fillArray(short original[],char choice, short size)
    : {
    : srand(time(NULL));
    :[red] //i is unitialized, num is uninitialized [/red]
    : short [red]i[/red],[red]num[/red];
    : for(i=0;i>original[[red]i[/red]];
    : break;
    : //also have tried cin>>num;original[i]=num;tried a for loop as
    : //well.
    :
    : case 'B':
    : case 'b': original[[red]i[/red]]=rand();//puts random numbers into array
    : break;
    :
    : }
    : }
    :
    :
    : }
    : [/code]
    :

    See code. This should get you on the road again.
    Good luck,
    bilderbikkel

  • texmommyof4texmommyof4 Posts: 13Member
    Ok, I should officially receive a big kick in the hiny.... the variable num was declared a short.. and dummy me not thinking was just throwing random numbers in there such 654987. So that was what was throwing my program for a loop (literally)!

    Thanks for your help though! I greatly appreciate it!

    Grace ;)
Sign In or Register to comment.