As with anyone else posting problems here, I am new to C++, and programming. I am currently in a logics class that is not language specific and use the RAPTOR flowchart program. I created a program and felt like trying to do the C++ code. I am using Code::Blocks and using this site as well as others as tutorials to help. I have been slowly writing parts of the program and testing it to make sure it is right. Currently though I am having an issue finding what is going wrong. The Build returns no error in the code, but when I run the program, it doesn't work. This is the source code:
#include <iostream>
#include <cstdlib>
#include <string>
#include <time.h>
usingnamespace std;
int main ()
{
int card[10], number[10];
string suit[10];
int k;
srand(time(0));
//Draw each card. RNG 1-52 with IF statements to prevent duplicates
for (k=1;k<11;k++){
card[k] = rand()%52+1;
switch (k){
case 1:
break;
case 2:
label2:
if (card[k]==card[1]){
card[k] = rand()%52+1;
goto label2;
}
break;
case 3:
label3:
if (card[k]==card[1]||card[k]==card[2]){
card[k] = rand()%52+1;
goto label3;
}
break;
case 4:
label4:
if (card[k]==card[1]||card[k]==card[2]||card[k]==card[3]){
card[k] = rand()%52+1;
goto label4;
}
break;
case 5:
label5:
if (card[k]==card[1]||card[k]==card[2]||card[k]==card[3]||card[k]==card[4]){
card[k] = rand()%52+1;
goto label5;
}
break;
case 6:
label6:
if (card[k]==card[1]||card[k]==card[2]||card[k]==card[3]||card[k]==card[4]||card[k]==card[5]){
card[k] = rand()%52+1;
goto label6;
}
break;
case 7:
label7:
if (card[k]==card[1]||card[k]==card[2]||card[k]==card[3]||card[k]==card[4]||card[k]==card[5]||card[k]==card[6]){
card[k] = rand()%52+1;
goto label7;
}
break;
case 8:
label8:
if (card[k]==card[1]||card[k]==card[2]||card[k]==card[3]||card[k]==card[4]||card[k]==card[5]||card[k]==card[6]
||card[k]==card[7]){
card[k] = rand()%52+1;
goto label8;
}
break;
case 9:
label9:
if (card[k]==card[1]||card[k]==card[2]||card[k]==card[3]||card[k]==card[4]||card[k]==card[5]||card[k]==card[6]
||card[k]==card[7]||card[k]==card[8]){
card[k] = rand()%52+1;
goto label9;
}
break;
case 10:
label10:
if (card[k]==card[1]||card[k]==card[2]||card[k]==card[3]||card[k]==card[4]||card[k]==card[5]||card[k]==card[6]
||card[k]==card[7]||card[k]==card[8]||card[k]==card[9]){
card[k] = rand()%52+1;
goto label10;
}
break;
} //end switch
} //end for
//Set each card's number and suit
for (k=1;k<11;k++){
if (card[k]<=13){
number[k]=card[k];
suit[k]="H";
}
elseif (card[k]<=26){
number[k]=card[k]-13;
suit[k]="D";
}
elseif (card[k]<=39){
number[k]=card[k]-26;
suit[k]="S";
}
else {
number[k]=card[k]-39;
suit[k]="C";
}
} //end for
for (k=1;k<11;k++){
cout << k <<". " <<card[k] <<": " <<number[k] <<" of " <<suit[k];
}
cin.get();
} //end main()
The program window returns :
Process returned -1073741819 (0xC0000005) execution time : 2.990 s
Before adding the card's number and suit, testing went fine and I was able to display 10 random numbers ("cards"), so I am assuming the error is somewhere in the second half, but can't be sure.
Also please forgive any crude coding. I've only been at this for 2 days.
I'm way less versed than you are, surmising by your code, but we have already studied switch statements. Every one we had to write included the default: case at the end. Hope that helps.
default: is not necessary for a switch loop to work fine. It just defines a case which is executed when no other case is valid. There are situation where the uncatched cases should not be treated so default: is unnecessary there.
@eypros
Thanks for teaching me about default within switch. I'm very new to C++ and just offer what I've learned in this first class I'm taking this semester.
gaah....thank you! That is the problem with having only used RAPTOR flowcharts. RAPTOR program is highly simplified and it starts arrays at 1 instead of 0.
As for the goto, I thought about trying a while loop last night.