this loop just wont work

why isn't the game loop looping
error says
control reaches the end of non-void function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//libraries
#include <cstdlib> //random number usage
#include <ctime> //random number usage
#include <iostream>
using namespace std;

//Programmer defined data types
//playing cards
struct PlayingCard
{
  int value;
  int suit;
}; //

//Special compiler dependent definitions
//NONE

//global constants/variables
const int NUM_PLAYERS = 2;

//Programmer defined functions
void getCard(PlayingCard *);
void getSuited(PlayingCard *);
bool getYesorNo();

//main program
int main()
{
  //Data
  srand(time(0)); //or else the sequence of random numbers will be the same every time your program runs
  PlayingCard player[NUM_PLAYERS];
  int index;
  int i = 0;

  // introduction
  cout << "Objective: This program will serve as a template for all programs\n written in this course.\n"; 
  cout << "Programmer: Teacher\n"; 
  cout << "Editor(s) used: Notepad\n"; 
  cout << "Compiler(s) used: TDM MinGW\n"; 
  cout << "File: " << __FILE__ << endl; 
  cout << "Complied: " << __DATE__ << " at " << __TIME__ << endl << endl;

  //
  do
  {
    for (index = 1; index < NUM_PLAYERS; index++) getCard(&player[index]);

    //
    cout << "Computer's card is "; 
    getSuited(&player[1]);
    cout << endl;
    cout << "Human's card is ";
    getSuited(&player[2]);
    cout << endl <<endl;

    //who won
    if (player[1].value > player[2].value) 
    {
      cout << "[Computer wins!] " <<endl;
      getYesorNo();
    }
    else if (player[1].value < player[2].value)
    {
      cout << "[Human wins!] " <<endl;
      getYesorNo();
    }
    else if (player[1].value == player[2].value)
    {
      cout << "[It's a tie...UNACCEPTABLE] " <<endl;
    } 
  }while (getYesorNo());
}//main

//
bool getYesorNo()
{
  char letsGo;
  bool getYesorNo = true;
  cout <<"Do you want to play again? [Y for yes/N for no] ";
  cin >> letsGo;
  cin.ignore(1000, 10);
  cout << endl;
  if (letsGo == 'n' || letsGo == 'N') return false;
  else if (letsGo == 'y' || letsGo == 'Y') return true;
  else return getYesorNo(); 
}//playAgn

//
void getCard(PlayingCard *a)
{
  //data
  srand(time(0)); //or else the sequence of random numbers will be the same every time your program runs
  int i;
  int u;
  for (i = 0; i < 13; i++)
  {
    for (u = 0; u < 4; u++)
    a[i].value = 2 + (rand() % 13);
    a[u].suit = rand() % 4;
  }
}

//
void getSuited(PlayingCard *a)
{
  //data
  //
  srand(time(0)); //or else the sequence of random numbers will be the same every time your program runs
  int i;
  i = rand() % 13;
  switch (a[i].value)
  {
    case 14:
      cout << "Ace";
      break;
    case 11:
      cout << "Jack";
      break;
    case 12:
      cout << "Queen";
      break;
    case 13:
      cout << "King";
      break;
    default:
      cout << a[i].value;
  }

  i = rand() % 4;
  switch (a[i].suit)
  {
    case 0:
      cout << " of Spades";
      break;
    case 1:
      cout << " of Diamonds";
      break;
    case 2:
      cout << " of Hearts";
      break;
    case 3:
      cout << " of Clubs";
  } // switch to determine card suit
}
Last edited on
Notice your bool function:

1
2
3
4
5
6
7
8
9
10
11
12
bool getYesorNo()
{
  char letsGo;
  bool getYesorNo = true;
  cout <<"Do you want to play again? [Y for yes/N for no] ";
  cin >> letsGo;
  cin.ignore(1000, 10);
  cout << endl;
  if (letsGo == 'n' || letsGo == 'N') return false;
  else if (letsGo == 'y' || letsGo == 'Y') return true;
//What if letsGo is 'L'?
}//playAgn 


If the user inputs anything other than n, N, y, or Y, then you'll reach the end of a bool returning function without returning anything!


Also, notice how on line 60, 65, and 71, those are not function calls. You forgot the "()" after the function name.
fixed what you said but now it just says

war2.cpp:91:26: error: 'getYesorNo' cannot be used as a function
91 | else return getYesorNo();
| ^
fixed it but now it outputs the question twice and doesnt care about the first asnwer

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//libraries
#include <cstdlib> //random number usage
#include <ctime> //random number usage
#include <iostream>
using namespace std;

//Programmer defined data types
//playing cards
struct PlayingCard
{
  int value;
  int suit;
}; //

//Special compiler dependent definitions
//NONE

//global constants/variables
const int NUM_PLAYERS = 2;

//Programmer defined functions
void getCard(PlayingCard *);
void getSuited(PlayingCard *);
bool getYesorNo();

//main program
int main()
{
  //Data
  srand(time(0)); //or else the sequence of random numbers will be the same every time your program runs
  PlayingCard player[NUM_PLAYERS];
  int index;
  int i = 0;

  // introduction
  cout << "Objective: This program will serve as a template for all programs\n written in this course.\n"; 
  cout << "Programmer: Teacher\n"; 
  cout << "Editor(s) used: Notepad\n"; 
  cout << "Compiler(s) used: TDM MinGW\n"; 
  cout << "File: " << __FILE__ << endl; 
  cout << "Complied: " << __DATE__ << " at " << __TIME__ << endl << endl;

  //
  do
  {
    for (index = 1; index < NUM_PLAYERS; index++) getCard(&player[index]);

    //
    cout << "Computer's card is "; 
    getSuited(&player[1]);
    cout << endl;
    cout << "Human's card is ";
    getSuited(&player[2]);
    cout << endl <<endl;

    //who won
    if (player[1].value > player[2].value) 
    {
      cout << "[Computer wins!] " <<endl;
      getYesorNo();
    }
    else if (player[1].value < player[2].value)
    {
      cout << "[Human wins!] " <<endl;
      getYesorNo();
    }
    else if (player[1].value == player[2].value)
    {
      cout << "[It's a tie...UNACCEPTABLE] " <<endl;
    } 
  }while (getYesorNo());
}//main

//
bool getYesorNo()
{
  char letsGo;
  bool getYesorNo = true;
  cout <<"Do you want to play again? [Y for yes/N for no] ";
  cin >> letsGo;
  cin.ignore(1000, 10);
  cout << endl;
  if (letsGo == 'n' || letsGo == 'N') return false;
  else if (letsGo == 'y' || letsGo == 'Y') return true;
  else return false; 
}//playAgn

//
void getCard(PlayingCard *a)
{
  //data
  srand(time(0)); //or else the sequence of random numbers will be the same every time your program runs
  int i;
  int u;
  for (i = 0; i < 13; i++)
  {
    for (u = 0; u < 4; u++)
    a[i].value = 2 + (rand() % 13);
    a[u].suit = rand() % 4;
  }
}

//
void getSuited(PlayingCard *a)
{
  //data
  //
  srand(time(0)); //or else the sequence of random numbers will be the same every time your program runs
  int i;
  i = rand() % 13;
  switch (a[i].value)
  {
    case 14:
      cout << "Ace";
      break;
    case 11:
      cout << "Jack";
      break;
    case 12:
      cout << "Queen";
      break;
    case 13:
      cout << "King";
      break;
    default:
      cout << a[i].value;
  }

  i = rand() % 4;
  switch (a[i].suit)
  {
    case 0:
      cout << " of Spades";
      break;
    case 1:
      cout << " of Diamonds";
      break;
    case 2:
      cout << " of Hearts";
      break;
    case 3:
      cout << " of Clubs";
  } // switch to determine card suit
}
That's because of line 60 and 65. You ask them if they want to play again, even though there's no reason to. The input they give in those function calls doesn't do anything. And then when the condition calls the function, it calls the function again which makes the user have to input their answer twice.
i see now how i was using it wrong thank you.

however i am still having issues getting this to run properly.

it should deal two random cards
take them out of the deck so they cant be played again
if computer value is larger computer wins
if user vaule is larger user wins
if its a tie play till there is a winner


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178

//libraries
#include <cstdlib> //random number usage
#include <ctime> //random number usage
#include <iostream>
using namespace std;

//Programmer defined data types
//playing cards
struct PlayingCard
{
  int value;
  int suit;
}; //

//Special compiler dependent definitions
//NONE

//global constants/variables
const int NUM_PLAYERS = 3;

//Programmer defined functions
void getCard(PlayingCard *, PlayingCard *);
void getSuited(PlayingCard *);
bool getYesorNo();

//main program
int main()
{
  //Data
  srand(time(0)); //or else the sequence of random numbers will be the same every time your program runs
  PlayingCard player[NUM_PLAYERS];
  PlayingCard deck;
  int index;
  int i = 0;

  // introduction
  cout << "Objective: This program will serve as a template for all programs\n written in this course.\n"; 
  cout << "Programmer: Teacher\n"; 
  cout << "Editor(s) used: Notepad\n"; 
  cout << "Compiler(s) used: TDM MinGW\n"; 
  cout << "File: " << __FILE__ << endl; 
  cout << "Complied: " << __DATE__ << " at " << __TIME__ << endl << endl;

  //
  do
  {
    do
    {
      int i = 0;
      for (index = 1; index < 2; index++) getCard(&player[index], &deck);
      i++;
      //
      cout << "Computer's card is "; 
      getSuited(&player[1]);
      cout << endl;
      cout << "Human's card is ";
      getSuited(&player[2]);
      cout << endl <<endl;

      //who won
      if (player[1].value > player[2].value) 
      {
        cout << "[Computer wins!] " <<endl <<endl;
        break;
      }
      else if (player[2].value > player[1].value) 
      {
        cout << "[Human wins!] " <<endl <<endl;
        break;
      }
      else if (player[1].value == player[2].value) 
      {
        cout << "[It's a tie...UNACCEPTABLE] " <<endl <<endl;
        cout << "Dealing AGAIN" <<endl;
        i = 0;
      } 
    }while (i != 1);
  }while (getYesorNo());
}//main

//
bool getYesorNo()
{
  char letsGo;
  bool getYesorNo = true;
  cout <<"Do you want to play again? [Y for yes/N for no] ";
  cin >> letsGo;
  cin.ignore(1000, 10);
  cout << endl;
  if (letsGo == 'n' || letsGo == 'N') return false;
  else if (letsGo == 'y' || letsGo == 'Y') return true;
  else return false; 
}//playAgn

//
void getCard(PlayingCard *a, PlayingCard *b)
{
  //data
  int d = 0;
  int i;
  bool iV = false;
  do
  {
    srand(time(0)); //or else the sequence of random numbers will be the same every time your program runs
    a[0].value = 2 + (rand() % 13);
    a[0].suit = rand() % 3;
    for (i = 2; i < 13; i++)
    {
      if (a[0].value && a[0].suit == b[i].value && b[0].suit) 
      {
        i = 0;
        d++;
      }
      else if (a[0].value && a[0].suit == b[i].value && b[1].suit)
      {
        i = 0;
        d++;
      }
      else if (a[0].value && a[0].suit == b[i].value && b[2].suit)
      {
        i = 0;
        d++;
      }
      else if (a[0].value && a[0].suit == b[i].value && b[3].suit)
      {
        i = 0;
        d++;
      }
      else
      {
        b[a[0].value].value = a[0].value;
        b[a[0].suit].suit = a[0].suit;
        iV = true;
        break;
      }//else
    }//for
  }while (iV == false);
}

//
void getSuited(PlayingCard *a)
{
  //data
  // 
  switch (a[0].value)
  {
    case 14:
      cout << "Ace";
      break;
    case 11:
      cout << "Jack";
      break;
    case 12:
      cout << "Queen";
      break;
    case 13:
      cout << "King";
      break;
    default:
      cout << a[0].value;
  }

  switch (a[0].suit)
  {
    case 0:
      cout << " of Spades";
      break;
    case 1:
      cout << " of Diamonds";
      break;
    case 2:
      cout << " of Hearts";
      break;
    case 3:
      cout << " of Clubs";
  } // switch to determine card suit
}
Last edited on
Why am I getting repeated outputs and why isn’t the user card changing, I can’t figure it out
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
void getCard(PlayingCard *a, PlayingCard *b)
{
  //data
  int d = 0;
  int i;
  bool iV = false;
  do
  {
    srand(time(0)); //don't reseed the generator, line 31 in main() is enough
    a[0].value = 2 + (rand() % 13); //`a' is not an array, confusing
    a[0].suit = rand() % 3; //may use a->value, a->suit
    for (i = 2; i < 13; i++)
    {
      if (a[0].value && a[0].suit == b[i].value && b[0].suit) //b is definitaly not an array, out of bounds access
      {
        i = 0;
        d++;
      }
      else if (a[0].value && a[0].suit == b[i].value && b[1].suit)
      //¿want to compare both members? then compare both members
      //a->value == b->value and a->suit == b->suit
      {
        i = 0;
        d++;
      }
      else if (a[0].value && a[0].suit == b[i].value && b[2].suit)
      {
        i = 0;
        d++;
      }
      else if (a[0].value && a[0].suit == b[i].value && b[3].suit)
      {
        i = 0;
        d++;
      }
      else
      {
        b[a[0].value].value = a[0].value; //¿what?
        b[a[0].suit].suit = a[0].suit;
        iV = true;
        break; //... ¿what's the point of iV then?
      }//else
    }//for
  }while (iV == false); //while (not iV); learn what is a boolean
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
void getCard(PlayingCard *a, PlayingCard *b)
{
  //data
  int d = 0;
  int i;
  do
  {
    a[0].value = 2 + (rand() % 13); //these are arrays in the defined to work in the struct form??
    a[0].suit = rand() % 3; //these are arrays in the defined to work in the struct form??
    for (i = 2; i < 13; i++)
    {
      if (a[0].value == b[i].value && a[0].suit == b[0].suit) //same thing i had but reworded??
      {
        i = 0;
        d++;
      }
      else if (a[0].value == b[i].value && a[0].suit == b[1].suit)//thing i had but reworded??
      {
        i = 0;
        d++;
      }
      else if (a[0].value == b[i].value && a[0].suit == b[2].suit)//same thing i had but reworded??
      {
        i = 0;
        d++;
      }
      else if (a[0].value == b[i].value && a[0].suit == b[3].suit) //same thing i had but reworded??
      {
        i = 0;
        d++;
      }
      else //if the user's current card does match any already played save that card
      {
        b[a[0].value].value = a[0].value; //save the value being output in the deck array so it can't be picked again
        b[a[0].suit].suit = a[0].suit; //save the vaule being output in the deck array so it cant be picked again
        break; //this has purpose now
      }//else
    }//for
  }while (false); // better?
}



it still wont pick a humans card at random still
Last edited on
Topic archived. No new replies allowed.