how can i fix these errors in c? - c

I keep getting these errors. Im trying to make a mine sweeper like game.
well.c: In function 'main':
well.c:170: warning: passing argument 1 of 'bombCheck' makes pointer from integer without a cast
well.c:170: warning: passing argument 3 of 'bombCheck' makes integer from pointer without a cast
well.c: In function 'fillGameBoard':
well.c:196: error: expected declaration or statement at end of input
#include <stdio.h>
#include <stdlib.h>
#define Rows 5
#define Columns 5
#define Bombs 5
void introduction(void)
{
puts("Welcome to the minefield!");
puts("In this level 2 game, you will win by choosing.");
puts("all of the viable wells and not any of the.");
puts("tool breaker spaces. In both games, there are.");
puts("20 viable spaces and 5 tool breakers!");
puts("Have fun and good luck!");
}
void fillGameBoard(char gameBoard[][Columns])
{
int i, j;
FILE *inputFile;
char gameDataFileName[30];
int yes = 0;
do
{
printf("choose your spot");
printf("\nfield1 field2\n");
scanf(" %s",&gameDataFileName);
if ((inputFile = fopen(gameDataFileName,"r")) == NULL)
{
puts("\nWrong input! Try again!");
puts("check spelling, spacing, etc. make it exact!");
}
else
{
yes = 1;
}
} while (yes == 0);
for (i=0; i<Rows; i++)
{
for (j=0; j<Columns; j++)
{
fscanf(inputFile, " %c", &gameBoard[i][j]);
}
fclose(inputFile);
return;
}
void fillUserBoard(char userBoard[][Columns])
{
int i,j; // counters
for (i=0; i<Rows; i++)
{
for (j=0; j<Columns; j++)
{
userBoard[i][j] = '~';
}
}
return;
}
void displayBoard(char board[][Columns])
{
int i, j;
printf("\n ");
for (i = 1; i <= Rows; i++)
{
printf("%d ",i+5);
}
puts("");
for (i = 0; i <=Rows; i++)
{
printf("__");
}
puts("");
for (i=0; i<Rows; i++)
{
printf("%d|",(i+1));
for (j=0; j<Columns; j++)
{
printf(" %c", board[i][j]);
}
puts("");
}
return;
}
char bombCheck (char board[][Columns], int a, int b)
{
char gameOver;
if (board[a][b] == '*')
{
puts("");
puts(" BOOM");
puts("You hit a mine.");
puts("you are deaded.\n");
puts(" GAME OVER!!\n");
gameOver = 'y';
}
else
{
gameOver = 'n';
}
return gameOver;
}
int main (void)
{
char gameBoard[Columns][Rows];
char userBoard[Columns][Rows];
char done;
char win;
char gameOver;
int count;
int i;
int col;
int row;
introduction();
do
{
done=win='n';
count=0;
fillGameBoard(gameBoard);
fillUserBoard(gameBoard);
displayboard(userBoard);
bombcheck();
do
{
displayBoard(userBoard);
printf("choose your column, numbered 1-5\n");
scanf(" %i", &col);
printf("choose your row, numbered 1-5\n");
scanf(" %i", &row);
done = bombCheck(col, row, gameBoard);
if (done='n')
{
count+1;
if (count==((Columns*Rows)-Bombs))
{
printf("you win!\n");
done='y';
}
else
{
done='n';
userBoard[col][row]=gameBoard[col][row];
}
}
} while (done != 'y');
printf("do you want to play again? y/n \n");
scanf(" %c", win);
}while (win != 'y');
return 0;
}

You're missing a brace in fillGameBoard().
for (i=0; i<Rows; i++)
{
for (j=0; j<Columns; j++)
{
fscanf(inputFile, " %c", &gameBoard[i][j]);
} /* Note closing brace! */
}
fclose(inputFile);
You're passing the arguments to bombCheck() in the wrong order.
/* Declared: char bombCheck (char board[][Columns], int a, int b) */
done = bombCheck(gameBoard, col, row);
What's with the bombcheck() call with no arguments? Note that bombcheck() is different from bombCheck(). The C programming language is case-sensitive.
For future reference, post only the minimal code snippets relevant to your question, instead of an entire program.

Case matters in C. bombcheck is not the same as bombCheck.
Argument order matters. You have declared bombCheck with (board, a, b) but are calling it with (col, row, board).
Taking the address of an array is redundant. The & is not necessary in scanf("%s",gameDataFileName);
scanf with %s is pretty unsafe. Watch what happens if you type in more than 30 characters (perhaps substantially more). Try fgets instead.
You're missing a closing brace in fillGameBoard (probably in the second inner for loop).
Your indenting is inconsistent in some places, particularly where you have left aligned return statements (and some other statements at the end of blocks).
Overall, this is a pretty good beginner program. Keep at it, and you'll get familiar with those compiler errors in no time!

There is a missing closing brace } to end the fillGameBoard function.

The number of opening and closing braces don't add up.

line 170: wrong order of arguments
line 51: missing }

Related

My code runs in VSCode but does not run in DevC

#include <stdlib.h>
#include <stdio.h>
void arraydescending(int array[]){
for (int j=0; j<9; j++){
for (int i=0; i<8; i++)
{
if(array[i]<array[i+1]){
int swapper = array[i+1];
array[i+1]=array[i];
array[i]=swapper;
}
}
}
for (int c=0; c<9; c++){
printf("%d",array[c]);
}
}
void arrayreverse(int array[])
{
for(int i = 0; i<4; i++)
{
int swapper = array[i];
array[i] = array[8-i];
array[8-i] = swapper;
}
for (int c=0; c<9; c++){
printf("%d",array[c]);
}
}
int main()
{
int choice;
printf("Please enter your choice:");
scanf("%d", &choice);
if(choice == 1)
{
int mynumberarray[9] = {1,1,0,2,0,0,0,4,7};
int choice_2;
printf("Write 1 for reverse order, write 2 for descending order:");
scanf("%d", &choice_2);
if(choice_2 == 1)
{
arrayreverse(mynumberarray);
}
else if(choice_2 == 2)
{
arraydescending(mynumberarray);
}
else
{
printf("Invalid choice");
}
}
else if(choice == 2){
int userarray[9];
char * user_entry;
printf("Please enter your school no (9 digits):");
scanf("%s",user_entry);
for(int i = 0; i < 9; i++)
{
userarray[i] = user_entry[i] - '0';
}
int choice_2;
printf("Write 1 for reverse order, write 2 for descending order:");
scanf("%d", &choice_2);
if(choice_2 == 1)
{
arrayreverse(userarray);
}
else if(choice_2 == 2)
{
arraydescending(userarray);
}
else
{
printf("Invalid choice");
}
}
else
{
printf("Invalid choice");
}
return 0;
}
This code runs correctly when I compiled it with gcc -std=c99; but my friend has DevC 5.11 version can compile the code but it doesn't run correctly in his DevC (It exits the program in the second scanf). Both compiles but why it does not run in DevC 5.11 with the compiler gcc 4.9.2? I am waiting for your suggestions because I didn't understand the reason behind it, my code looks like it has not any mistakes.
Your program has undefined behavior at least because in this code snippet
char * user_entry;
printf("Please enter your school no (9 digits):");
scanf("%s",user_entry)
you are using the uninitialized pointer user_entry that has an indeterminate value. You need to declare a large enough character array where you are going to read a string of digits. Do not forget to reserve in the array a space for the terminating zero character of the read string.

Concatenation of first letter alike regardless of upper and lower case in C

i have been figuring out how to combine strings that has first same letter alike regardless of their cases. I have a code that if you input
babe,two,Bird,Tea
the output is always like this
babe,Bird,Tea,two
But the input that I want to see is like this
babeBird,Teatwo
What am i going to add or change in my code in order for me to do that. Here's my code:
#include<stdio.h>
#include<string.h>
#include <ctype.h>
int main()
{
char str1[1000][1000], str[1000], temp[1000];
int n, i, p, j, a;
char *ptr, *ptr1, letter;
printf("Enter how many arrays: ");
scanf("%d", &n);
for(i=0; i<n; i++)
{
printf("Enter string %d: ", i+1);
scanf("%s", &str1[i]);
}
for(i=0; i<n-1; i++)
{
for(j=i+1; j<n; j++)
{
if (tolower((unsigned char)str1[i][0]) == tolower((unsigned char)str1[j][0])
){
strcpy(temp, str1[i]);
strcpy(str1[i], str1[j]);
strcpy(str1[j], temp);
}
if(strcasecmp(str1[i], str1[j])>0)
{
strcpy(temp, str1[i]);
strcpy(str1[i], str1[j]);
strcpy(str1[j], temp);
}
}
}
for(i=0; i<n; i++)
{
if (i != 0)
{
else if (str1[i][0] != letter)
{
printf(",");
}
}
{
printf("%s", str1[i]);
letter = str1[i][0];
}
}
}
The following code which decides whether to print a comma
if (str1[i][0] != letter)
does a case-sensitive comparison.
Change it to something like
if (tolower(str1[i][0]) != tolower(letter))
However, the code contains multiple bugs (too many to point them all out), so not sure it will work. You might want to do debugging.

I have following code for image provided in the link.After n=2 I cant iterate spaces,please guide me through it

I am new to stackoverflow so please forgive for any mistakes.
#include void main() { int i,j,p,n,s; printf("Enter rows:\n"); scanf("%d",&n); if(n==1) { for(i=1;i<=n+2;i++) { for(j=1;j<=i;j++) { printf("* "); } printf("\n"); } } else { for(i=1;i<=3;i++) { for(j=1;j<=i;j++) { printf("* "); } printf("\n"); } for(p=1;p<=n-1;p++) { for(i=1;i<=2;i++) { for(j=3;j<=3;j++) { printf(" "); for(j=1;j<=i+1;j++) { printf("* "); } } printf("\n"); } } } }
here is how the code should be posted:
With 4 spaces infront of each line of code
#include <stdio.h>
int main( void )
{
int i,j,p,n,s;
printf("Enter rows:\n");
scanf("%d",&n);
if(n==1)
{
for(i=1;i<=n+2;i++)
{
for(j=1;j<=i;j++)
{
printf("* ");
}
printf("\n");
}
}
else
{
for(i=1;i<=3;i++)
{
for(j=1;j<=i;j++)
{
printf("* ");
}
printf("\n");
}
for(p=1;p<=n-1;p++)
{
for(i=1;i<=2;i++)
{
for(j=3;j<=3;j++)
{
printf(" ");
for(j=1;j<=i+1;j++)
{
printf("* ");
}
}
printf("\n");
}
}
}
}
However, when your code (and the above) is run through the compiler, this is the result:
gcc -ggdb -Wall -Wextra -Wconversion -pedantic -std=gnu11 -c "untitled.c"
untitled.c: In function ‘main’:
untitled.c:5:14: warning: unused variable ‘s’ [-Wunused-variable]
int i,j,p,n,s;
^
Compilation finished successfully.
You really do not want your code to be producing any warnings, nor any errors.
for ease of readability and understanding:
consistently indent the code. Indent after every opening brace '{'. Unindent before every closing brace '}'. Suggest each indent level be 4 spaces.
follow the axiom: only one statement per line and (at most) one variable declaration per statement.
variable names (and parameter names) should indicate content or usage (or better, both)
insert a reasonable space inside parens, inside brackets, inside braces, after commas, after semicolons, around C operators
Note there are only two valid signatures for the main() function. Both those signatures have a return type of int
do NOT post images. Rather, copy and past the actual text into your question
What do you mean by: cannot iterate spaces?
Wow.
It took me a bit of time to actually understand what you wish.
Below you can see the indented, commented, and working code. I tried to keep as much of it as I can.
#include <stdio.h>
int main() {
int i, /* used for "* " when n == 1 (columns),
used for counting rows for the first block when n != 1*/
j, /* used for "* " when n != 1 (columns),
used for counting rows for the first block when n != 1*/
p, /* used to print all the blocks after n = 1, when n != 1 */
n, /* input from user; number of blocks to print */
s; /* unused !!!! */
printf("Enter rows:\n");
scanf("%d", &n);
// WARNING! 'n' can be zero. That would break this whole thing.
if(n==1) {
for(i = 1; i <= n + 2; i++) {
for(j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}
} else {
// Draw first block; 'i' = rows, 'j' = columns
// This could be solved with only 'i'...
for(i = 1; i <= 3; i++) {
for(j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}
// Draw the latter blocks
// Could simpy do "p = 0; p <= n", instead?
for(p = 1; p <= n-1; p++) {
// Loop twice, second and 2rd row of the block:
//
// * *
// * * *
for(i = 1; i <= 2; i++) {
// Loop once? What?!
for(j = 3; j <= 3; j++) {
// I inserted this here; this solves your problem, "indentation" works fine
// I actually make use of 's'! Yiyy!
for (s = 1; s <= p; s++) {
printf(" ");
}
// Draw the columns
for(j = 1; j <= i + 1; j++) {
printf("* ");
}
}
printf("\n");
}
}
}
}
I completely agree with User3629249's answer, try to follow the rules for readability.
You might also want to try debuggers, be it the Visual Studio Debugger, or gdb. Also, variable names, please!
Have a nice day,
- COlda

How to access data from an array of strings? C

I am having lot of problems with my C code. I am saving data into my array with this function:
/**
* Insert Data into array
* #param char[] pointer
*/
void insertData(char **data) {
int i;
for (i = 0; i < 2; i++) {
data[i] = malloc(10000);
printf("Nombre del paciente %d: ", i+1);
scanf("%s", &data[i][0]);
printf("Habitacion: ");
scanf("%s", &data[i][1]);
printf("Cama: ");
scanf("%s", &data[i][2]);
free(*data);
}
}
My variable data is : char data[2][3];
And I'm trying to show this data with the next function:
void mostrarResultados(char **data) {
int i,j;
for (i = 0; i < 2; i++) {
printf("\n");
for (j = 0; j < 3; j++) {
printf("%c ", data[i,j], **data);
}
}
}
But the console return me weird characters:
What am I doing wrong?
To these functions I am calling them this way:
//First menu
do{
switch (option){
case 1:
insertData(data);
setFirstTime(false);
mostrarResultados(data);
break;
case 2:
exit(0);
break;
}
} while (option == 0);
}
Thank you very much for your help!
I think you better can use a struct to hold the data. Someting like
Struct data {
char nombre[32];
Char habitacion[32];
char cama[32];
}
And malloc the struct malloc(sizeof(struct data) * nr of struct you want)
When programming in C or C++, you must enable compiler warnings, understand them and fix them properly. Everything else is irresponsible.
In this case, the compiler will warn about the printf call, since you are passing the wrong type (and number) of arguments.

C: Format specifies type 'char *'

Whenever I run the following code, I get the error message
"format specifies type char * but the argument has type int."
The program is supposed to print a n by n square or triangle of a specific character. I'm pretty new to C, and I haven't had much luck troubleshooting this.
#include <stdio.h>
#include <ctype.h>
void print_square(int n, char c) {
for (int i=0; i < n; i++) {
for (int j; j < n; j++) {
printf("%c", c);
}
printf("\n");
}
}
void print_triangle(int n, char c) {
int count = 1;
for (int i=0; i < n; i++) {
for (int j; j < count; j++) {
printf("%c", c);
}
count = count + 1;
printf("\n");
}
}
int main(int argc, const char * argv[]) {
int n;
char cmd;
char * c;
do {
printf("Enter T for a triangle, S for a square, "
"Q to quit: ");
scanf("%c", &cmd);
cmd = toupper(cmd);
if (cmd == 'S' || cmd == 'T') {
printf("Enter the size: ");
scanf("%d", &n);
printf("Enter the character: ");
scanf("%c", *c); // error here
if (cmd == 'S') {
print_square(n, *c);
}
else {
print_triangle(n, *c);
}
}
} while (cmd != 'T' && cmd != 'S' && cmd != 'Q');
return 0;
}
As you've pointed already, the error is indeed in
scanf("%c", *c);
You need to pass a valid pointer to char, why to dereference?
Note: In your case, you're dereferencing an unitialized pointer, which invokes undefined behavior, anyway.
To have a better approach (you dont really need c to be a pointer there) do something like
char c;
scanf(" %c", &c); //the leading space consumes the newline in input bufer
and you should be good to go.
Accordingly, you need to pass c instead of *c, as required in other function calls.

Resources