I am trying to make a tic tac toe game in c++, but I am getting the following errors in my code(Not completed yet). am I passing the arrays to the function correctly
[Error] invalid conversion from 'char(*)[3]'to 'char'
and
[Error] initializing argument 1 of 'void player_input(char)'
#include <iostream>
using namespace std;
void draw_grid(char display[][3]){
cout<<"Board\n"<<"-------------"<<endl;
for(int x=0;x<3;x++){
for(int y=0;y<3;y++){
cout<<"| "<<display[x][y]<<" ";
}
cout<<"|"<<endl<<"-------------";
cout<<endl;
}
}
void player_input(char);
int main(){
char arr[3][3]={
{'1','2','3'},
{'4','5','6'},
{'7','8','9'}
};
draw_grid(arr);
player_input(arr);
draw_grid(arr);
return 0;
}
void player_input(char size[][3]){
char sign;
char number;
cout<<"X or O"<<endl;
cin>>sign;
cout<<"Choose a number\n";
cin>>number;
switch (number){
case '1':
size[0][0]=sign;
break;
case '2':
size[0][1]=sign;
break;
case '3':
size[0][3]=sign;
break;
case '4':
size[1][0]=sign;
break;
case '5':
size[1][1]=sign;
break;
case '6':
size[1][2]=sign;
break;
case '7':
size[2][0]=sign;
break;
case '8':
size[2][1]=sign;
break;
case '9':
size[2][2]=sign;
break;
}
}
void player_input(char);
Your function definition doesn't match the one below void player_input(char size[][3]). Before execution, the compiler tries to match each function call with its appropriate definition. It fails as it only sees the one above (which takes a single character), being matched with player_input(arr); with arr being a 2D array char arr[3][3].
You can modify the line above to make it work:
void player_input(char[3][3]);
Since you're using C++, you should ditch static arrays and consider using one of the modern C++ containers like std::vector.
Here's the fix:
#include <iostream>
using namespace std;
void draw_grid(char display[][3]){
cout<<"Board\n"<<"-------------"<<endl;
for(int x=0;x<3;x++){
for(int y=0;y<3;y++){
cout<<"| "<<display[x][y]<<" ";
}
cout<<"|"<<endl<<"-------------";
cout<<endl;
}
}
void player_input(char size[][3]){
char sign;
char number;
cout<<"X or O"<<endl;
cin>>sign;
cout<<"Choose a number\n";
cin>>number;
switch (number){
case '1':
size[0][0]=sign;
break;
case '2':
size[0][1]=sign;
break;
case '3':
size[0][3]=sign;
break;
case '4':
size[1][0]=sign;
break;
case '5':
size[1][1]=sign;
break;
case '6':
size[1][2]=sign;
break;
case '7':
size[2][0]=sign;
break;
case '8':
size[2][1]=sign;
break;
case '9':
size[2][2]=sign;
break;
}
}
int main(){
char arr[3][3]={
{'1','2','3'},
{'4','5','6'},
{'7','8','9'}
};
draw_grid(arr);
player_input(arr);
draw_grid(arr);
return 0;
}
You had 2 declarations of function player_input(). Also if you are not declaring the function type inside main(), you need to place the function definition before main().
Related
void getDay() {
bool repeat;
do
{
cout << "Enter the day code (first 2 letters): ";
cin >> weekDay1;
cin >> weekDay2;
weekDay1 = toupper(weekDay1);
weekDay2 = toupper(weekDay2);
switch (weekDay1)
{
case 'M':
break;
case 'T':
break;
case 'W':
break;
case 'F':
break;
case 'S':
break;
default:
cout << "Invalid input. Please try again.\n";
repeat = true;
break;
}
switch (weekDay2)
{
case 'O':
break;
case 'U':
break;
case 'E':
break;
case 'H':
break;
case 'R':
break;
case 'A':
break;
default:
cout << "Invalid input. Please try again.\n";
repeat = true;
break;
}
}while (repeat == true);
return;
}
I need this function to run once, and loop if the input is not one of the accepted characters. I'm trying to prevent any bad input, but it loops infinitely if the input entered on the initial run is not accepted. It works fine if the input is good on the first run, but I keep getting run-time errors for not initializing bools and I need some help adjusting this control.
The condition in the while loop is always true because you never set it to false in its body. You can do something like this:
void getDay() {
// Initializing while declaring is a good practice.
bool repeat = false;
do {
.
.
repeat = false;
.
switch(...) {
...
}
} while (repeat);
}
Now, repeat = true is only called if one of the switch statements invokes default.
I did a program that translates number into roman numbers, and I was about to show it to a friend of mine when the .exe file didn't work. It asked the user which number to put in, and after he put it the program closed. What I don't understand is why it did work after compiling on DevC++, but had a different behavior as a .exe
I found few solutions out there, and the one that worked for me was to add a:
int a;
cin>>a;
Before returning a cero. Now it works. I don't understand how the console doesn't execute the actions given without that. I'm leaving the code here.
#include <iostream>
using namespace std;
int main(){
int numero, unidades, decenas, centenas, millares;
int comprobante;
cout<<"Este programa traduce un numero de 4 cifras a numeros romanos.\nDigite un numero de
maximo cuatro cifras: ";
cin>>numero;
unidades= numero%10;
numero /= 10;
decenas= numero%10;
numero/=10;
centenas= numero%10;
numero/=10;
millares=numero;
comprobante=millares/10;
if(comprobante==0){
cout<<"Su numero traducido a numeros romanos es: ";
switch(millares){
case 1: cout<<"M"; break;
case 2: cout<<"MM"; break;
case 3: cout<<"MMM"; break;
}
switch(centenas){
case 1: cout<<"C"; break;
case 2: cout<<"CC"; break;
case 3: cout<<"CCC"; break;
case 4: cout<<"CD"; break;
case 5: cout<<"D"; break;
case 6: cout<<"DC"; break;
case 7: cout<<"DCC"; break;
case 8: cout<<"DCCC"; break;
case 9: cout<<"CM"; break;
}
switch(decenas){
case 1: cout<<"X"; break;
case 2: cout<<"XX"; break;
case 3: cout<<"XXX"; break;
case 4: cout<<"XL"; break;
case 5: cout<<"L"; break;
case 6: cout<<"LX"; break;
case 7: cout<<"LXX"; break;
case 8: cout<<"LXXX"; break;
case 9: cout<<"XC"; break;
}
switch(unidades){
case 1: cout<<"I"; break;
case 2: cout<<"II"; break;
case 3: cout<<"III"; break;
case 4: cout<<"IV"; break;
case 5: cout<<"V"; break;
case 6: cout<<"VI"; break;
case 7: cout<<"VII"; break;
case 8: cout<<"VIII"; break;
case 9: cout<<"IX"; break;
}
}
else{
cout<<"Te dije de 4 cifras. Por desgraciado me cierro.";
}
int a;
cin>>a;
return 0;
}
The program closes immediately in the terminal when the program reaches completion. When you cin>>a it is waiting for input before closing. See this.
Include
#include<conio.h>
And add getch(); at the end of your code. This will make your console to not close.
Your code was totally fine.
It is that when the main() reached return 0; that means it is done. And if you are running it as an .exe file, that means the program is done. And when a program is done, it would be closed.
There are couple ways to make it stay not closed. One that many have mentioned is to add a cin >> something; or getch(); with #include <conio.h> for windows at the end, so it would wait till you enter something.
Also you could delay the program before it reaches the return statement. You can check here How do you add a timed delay to a C++ program?
You could also wrap you entire program with a while loop, like:
while(true)
{
// Your programs here
if(someCondition) break;
};
where someCondition is a bool type, maybe char c; cin >> c; bool someCondition = (c == 'x'); So that the program only ends if someone input 'x', else it would run again.
#include <string>
#include <iostream>
using namespace std;
int main() {
string input, numBin = "";
cout << "Enter a hexadecimal number: ";
getline(cin, input);
for (int i = 0; i < input.length(); i++) {
switch (input[i]) {
case 0: numBin.append("0000"); break;
case 1: numBin.append("0001"); break;
case 2: numBin.append("0010"); break;
case 3: numBin.append("0011"); break;
case 4: numBin.append("0100"); break;
case 5: numBin.append("0101"); break;
case 6: numBin.append("0110"); break;
case 7: numBin.append("0111"); break;
case 8: numBin.append("1000"); break;
case 9: numBin.append("1001"); break;
case 'a': numBin.append("1010"); break;
case 'A': numBin.append("1010"); break;
case 'b': numBin.append("1011"); break;
case 'B': numBin.append("1011"); break;
case 'c': numBin.append("1100"); break;
case 'C': numBin.append("1100"); break;
case 'd': numBin.append("1101"); break;
case 'D': numBin.append("1101"); break;
case 'e': numBin.append("1110"); break;
case 'E': numBin.append("1110"); break;
case 'f': numBin.append("1111"); break;
case 'F': numBin.append("1111"); break;
default: break;
}
}
cout << "Your number in binary is " << numBin << ".";
}
This program is supposed to change a hexadecimal input ('input') into a binary result ('numBin'). I don't have much experience using switch statements and do not fully understand the "default" case, so any clarification about that or if I am using it incorrectly would be helpful!
The error I'm getting is on the for loop, and it thorws: comparison between signed and unsigned integer expressions [-Wsign-compare]
In the line:
for (int i = 0; i < input.length(); i++) ...
input.length() returns a size_t, which is a unsigned type.
(see http://www.cplusplus.com/reference/string/string/length/)
Comparing signed and unsigned values is not safe, which is why the compiler warns you, read more about it in this post among many others:
A warning - comparison between signed and unsigned integer expressions
To fix it, simply change to
unsigned int i = 0
The default switch case will be executed when none of the other cases match. You should put some code there that handles incorrect input for example.
case '0':
case '1':
...
Use all characters..not number and characters.
And one ore thing..for(i=0;i<(int) input.length();i++)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I have made a simple encryption function,which encrypts everything except 0-9 numbers (ignoring the special characters).
Here is the code.
#include <iostream>
using namespace std;
void encrypt(char s[])
{
char *ptr;
ptr=s;
while(*ptr)
{
switch (*ptr)
{
case 'a': *ptr='b';
break;
case 'b': *ptr='a';
break;
case 'c': *ptr='z';
break;
case 'd': *ptr='y';
break;
case 'e': *ptr='c';
break;
case 'f': *ptr='d';
break;
case 'g': *ptr='x';
break;
case 'h': *ptr='g';
break;
case 'i': *ptr='i';
break;
case 'j': *ptr='h';
break;
case 'k': *ptr='f';
break;
case 'l': *ptr='j';
break;
case 'm': *ptr='q';
break;
case 'n': *ptr='o';
break;
case 'o': *ptr='p';
break;
case 'p': *ptr='m';
break;
case 'q': *ptr='n';
break;
case 'r': *ptr='l';
break;
case 's': *ptr='k';
break;
case 't': *ptr='x';
break;
case 'u': *ptr='w';
break;
case 'v': *ptr='u';
break;
case 'w': *ptr='v';
break;
case 'x': *ptr='t';
break;
case 'y': *ptr='s';
break;
case 'z': *ptr='r';
break;
case 1: *ptr=5;
break;
case 2: *ptr=6;
break;
case 3: *ptr=0;
break;
case 4: *ptr=1;
break;
case 5: *ptr=2;
break;
case 6: *ptr=7;
break;
case 7: *ptr=4;
break;
case 8: *ptr=3;
break;
case 9: *ptr=8;
break;
case 0: *ptr=9;
break;
default: *ptr=*ptr;
break;
}
*ptr++;
}
*ptr='\0';
}
int main()
{
char password[10];
cout<<"Enter the password\n";
cin>>password;
encrypt(password);
cout<<password<<endl;
return 0;
}
Here is a sample output
sh-4.3$ main
Enter the password
thisisanex!!1234567
xgikikboct!!1234567
You need to use the character '1' not the integer value 1.
So use case '1': instead of case 1: and so on for the other numbers.
As nos said, the character is different from the integer value.
However, looking at your code, it would make a lot more sense to do this:
#include <iostream>
#include <string>
using namespace std;
int main(){
string charset = "abcdefghijklmnopqrstuvwxyz1234567890";
string scrambledcharset = "r5b6ng1fcl8htau9i74kxy0vjw3psemqz2do"; //Whatever order you want
string uIn;
string output;
cout << "Enter your string: ";
cin >> uIn;
cin.ignore();
for(int i = 0; i < uIn.length(); i++){
for(int j = 0; j < charset.length(); j++){
if(uIn[i] == charset[j]){
output += scrambledcharset[j];
}
}
}
cout << "\nScrambled: " << output;
return 0;
}
I have finished my program about encryption. The problem is, I need a shortcut. The program I have coded is too long and not very suitable. I'm just new to c++ programming. Can someone help me out? thanks! :) Here's my program:
#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main()
{
c:char crypt[25], cons;
int ctr;
cout<<"Input your 26-character cipherstring below.\n\n";
cout<<"ABCDEFGHIJKLMNOPQRSTUVWXYZ\n";cin>>crypt;
e:char string[65535];
cout<<"\nInput your string (input your spaces as any non-alphabet character).\n";cin>>string;
cout<<"\n\nEncrypted string: "; //std::string
for(ctr=0;ctr<=strlen(string);ctr++)
{
switch(string[ctr])
{
case('a'): cout<<crypt[0]; break;
case('b'): cout<<crypt[1]; break;
case('c'): cout<<crypt[2]; break;
case('d'): cout<<crypt[3]; break;
case('e'): cout<<crypt[4]; break;
case('f'): cout<<crypt[5]; break;
case('g'): cout<<crypt[6]; break;
case('h'): cout<<crypt[7]; break;
case('i'): cout<<crypt[8]; break;
case('j'): cout<<crypt[9]; break;
case('k'): cout<<crypt[10]; break;
case('l'): cout<<crypt[11]; break;
case('m'): cout<<crypt[12]; break;
case('n'): cout<<crypt[13]; break;
case('o'): cout<<crypt[14]; break;
case('p'): cout<<crypt[15]; break;
case('q'): cout<<crypt[16]; break;
case('r'): cout<<crypt[17]; break;
case('s'): cout<<crypt[18]; break;
case('t'): cout<<crypt[19]; break;
case('u'): cout<<crypt[20]; break;
case('v'): cout<<crypt[21]; break;
case('w'): cout<<crypt[22]; break;
case('x'): cout<<crypt[23]; break;
case('y'): cout<<crypt[24]; break;
case('z'): cout<<crypt[25]; break;
case('A'): cout<<crypt[0]; break;
case('B'): cout<<crypt[1]; break;
case('C'): cout<<crypt[2]; break;
case('D'): cout<<crypt[3]; break;
case('E'): cout<<crypt[4]; break;
case('F'): cout<<crypt[5]; break;
case('G'): cout<<crypt[6]; break;
case('H'): cout<<crypt[7]; break;
case('I'): cout<<crypt[8]; break;
case('J'): cout<<crypt[9]; break;
case('K'): cout<<crypt[10]; break;
case('L'): cout<<crypt[11]; break;
case('M'): cout<<crypt[12]; break;
case('N'): cout<<crypt[13]; break;
case('O'): cout<<crypt[14]; break;
case('P'): cout<<crypt[15]; break;
case('Q'): cout<<crypt[16]; break;
case('R'): cout<<crypt[17]; break;
case('S'): cout<<crypt[18]; break;
case('T'): cout<<crypt[19]; break;
case('U'): cout<<crypt[20]; break;
case('V'): cout<<crypt[21]; break;
case('W'): cout<<crypt[22]; break;
case('X'): cout<<crypt[23]; break;
case('Y'): cout<<crypt[24]; break;
case('Z'): cout<<crypt[25]; break;
default: cout<<" "; break;
}
}
cout<<"\n\n";
/*cout<<"Input 'c' to re-input your cipherstring.\n 'e' to reuse your cipherstring.\n 'q' to quit. ";
comm:cout<<"\nCommand: "; cin>>cons;
switch (cons)
{
case('c'): cout<<endl; goto c; break;
case('C'): goto c; break;
case('e'): goto e; break;
case('E'): goto e; break;
case('q'): break;
case('Q'): break;
default: cout<<"Invalid command. Please refer to the command list above.\n";goto comm;
}*/
system("PAUSE"); return 0;
}
You can replace that huge switch statement with this:
if (isalpha(string[ctr]))
{
int index = toupper(string[ctr]) - 'A';
cout << crypt[index];
}
else
cout << " ";
Also, do not call strlen for every iteration of the for loop, just do it once:
for(int ctr=0, len=strlen(string); ctr<len; ctr++)
{
....
}
A few other things of note:
crypt is too small, it should have size 27
If you use getline instead of operator>>, you can have the user enter spaces properly
Your code is prone to buffer overflows. Use std::string and you can pretty much forget about those.
Note, that as Kerrek says, this assumes that in the character set you are using, the uppercase letters A-Z are in sequence. But I think that the cases where this is not true are so exceedingly rare, that you can safely ignore them.