Unresolved external symbol referenced in main - c

I have to create a program that has a function charster() that accepts three arguments: a character and two integers. It will print he character input x number of times per y number of lines. I am getting syntax error LNK2019 and LNK1120 Unresolved external symbol referenced in main. And IDK what I have done to cause this or what to do to correct it.
Here is my code:
#include <stdio.h>
void charster(char n, int n_char, int n_lines);
int main(void)
{
char n;
int n_lines, n_chars;
printf("Please enter a character to be printed, the amount of times it should appear, and the amount of lines that should appear: ");
while (scanf("%c, %d, %d", &n, &n_chars, &n_lines) == 3)
{
charster(n, n_chars, n_lines);
}
return 0;
void charster(char n, int n_char, int n_lines);
{
int count;
for (count = 0; count < n_lines; count++)
{
putchar(n);
}
}
}

Firstly C language does not support nested functions. So you have to write the function charster() outside the main function.
You can write it above or below the main function as prototype is already declared.
Kindly have a look at the following code:
#include<stdio.h>
void charster(char n, int n_char, int n_lines);
int main(void)
{
char n;
int n_lines, n_chars;
printf("Please enter a character to be printed, the amount of times it should appear, and the amount of lines that should appear: ");
while (scanf("%c %d %d", &n, &n_chars, &n_lines) == 3)
{
charster(n, n_chars, n_lines);
}
return 0;
}
void charster(char n, int n_char, int n_lines)
{
int times;
int lines;
for (lines = 0; lines < n_lines; lines++)
{
for(times=0;times<n_char;times++){
printf("%c ", n);
}
printf("\n");
}
}

Related

C doesn't open functions

So, my C doesn't want to open my functions. I don't know where to look further as I don't have any idea what I did wrong in this one. It works fine with another exercise.
Here's what I've written:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int hello();
int sum(int);
int k, i, getal1, getal2;
int goobye(void);
int main(void)
{
int hello();
for (i = 0; i < 5; i++)
{
printf("Give 2 numbers <100 : ");
scanf("%d%*c%d%*c", &getal1, &getal2);
int sum(int k);
}
int goodbye(void);
}
int hello()
{
printf("Welcome, this program will ask you to solve 1 sum");
return 0;
}
int sum(int k)
{
int som, som2;
som = getal1 + getal2;
printf("What is the sum of %d %d? %d", getal1, getal2);
scanf("%d", &som2);
if (som == som2)
{
printf("According to you, the sum of %d and %d equals %d. That is correct", getal1, getal2, som2);
}
else
{
printf("According to you, the sum of %d and %d equals %d. That is not correct", getal1, getal2, som2);
}
return 0;
}
int goobye(void)
{
printf("Thanks for your cooperation.");
return 0;
}
Thanks in advance!
in main function you should call a function without type
int main(void)
{
hello();
for (i = 0; i < 5; i++)
{
printf("Give 2 numbers <100 : ");
scanf("%d%*c%d%*c", &getal1, &getal2);
sum(k);
}
goodbye();
}
Edit the main function like this
Calls are not perform because you don't call the functions, you just redeclare them. This is probably a copy/paste error, but in the end, the calls don't happen.
I modified you code hunk and added comments
int main(void)
{
int hello(); //here is a forward declaration of hello()
hello(); // calls hello
for (i = 0; i < 5; i++)
{
printf("Give 2 numbers <100 : ");
scanf("%d%*c%d%*c", &getal1, &getal2);
int sum(int k); // forward declaration of sum()
sum(k); //call sum on k
}
int goodbye(void); // forward declaration of goodbye
goodbye(); //call goodbye
//careful your original code has a typo; you wrote goobye()
}
Firstly you need to read about the syntax while calling functions.
Also why you assign a certain return type to a certain function i.e. the difference between int hello(); and void hello();.
Also you have placed return 0 statement at multiple places which would end your program before you actually want it to!
#include <stdio.h>
void hello();
int sum(int);
int k, i, getal1, getal2;
void goobye(void);
int main(void)
{
hello(); //no return type mention when calling a function
for (i = 0; i < 5; i++)
{
printf("Give 2 numbers <100 : ");
scanf("%d%*c%d%*c", &getal1, &getal2);
sum(getal1, getal2);//you wish to calculate the sum of these two numbers i suppose
}
goodbye();
return 0;
}
void hello()
{
printf("Welcome, this program will ask you to solve 1 sum");
// return 0 here will end your program
//return 0;
}
int sum(int getal1, int getal2)
{
int som, som2;
som = getal1 + getal2;
printf("What is the sum of %d %d? %d", getal1, getal2);
scanf("%d", &som2);
if (som == som2)
{
printf("According to you, the sum of %d and %d equals %d. That is correct", getal1, getal2, som2);
}
else
{
printf("According to you, the sum of %d and %d equals %d. That is not correct", getal1, getal2, som2);
}//again the same issue with return 0 as above
//return 0;
}
void goobye(void)
{
printf("Thanks for your cooperation.");//again the same issue with return 0 as above
//return 0;
}

Error: Segmentation Fault 11 in Palindrome

So I have been trying to make sure that I can check whether an user-inputted word is a palindrome or not by using function prototypes. However, I am getting an error in the end saying "Segment Fault: 11". I am fairly new to using function prototypes so if anyone can help me with solving anything that can be going on in the body of the function definition, then please point it out to me.
#include <stdio.h>
void palindrome_check(int ch_length, char text)
int main(void)
{
int length;
printf("Enter how many characters are in the message: ");
scanf("%d", &length);
char m;
printf("Enter the message: ");
scanf("%c", &text);
palindrome_check(l, m);
return 0;
}
void palindrome_check(int ch_length, char text)
{
char msg[ch_length];
text = msg[ch_length];
int count = 0;
while (count < ch_length)
{
count++;
scanf("%c", &msg[count]);
}
int i, j;
for (i = 0; i < ch_length; i++)
{
msg[j] = msg[ch_length - i];
}
if (text[i] == text[j])
{
printf("The message you entered is a palindrome!\n");
}
else
{
printf("It's not a palindrome.\n");
}
}
I couldn't understand some of your code it seemed you were doing some unnecessary things. What was msg for? This should work if I understood your problem correctly:
#include <stdio.h>
#include <string.h>
void palindrome_check(int ch_length, char text []);
int main(void)
{
char text [100];
/*
int length;
printf("Enter how many characters are in the message: ");
scanf("%d", &length);
Not necessary if you use strlen()*/
printf("Enter the message: ");
fgets(text,100,stdin); /*Using fgets() to allow spaces input*/
/*Strip the newline*/
text [strlen(text)-1]='\0';
palindrome_check(strlen(text),text);
return 0;
}
void palindrome_check(int ch_length, char text [])
{
int i;
for (i = 0; i < ch_length; i++)
{
if(text [i] != text [ch_length-i-1])
{
printf("It's not a palindrome.\n");
return;
}
}
printf("It is a palindrome!\n");
}

Error when reading user input and storing into array

I have encounter an error when running this code. There will be a "program stop working" windows once it reaches the loop to scan for user input for the names ("scanf_s(%s", &nameptr[i]); ).
Any help or advice will be greatly appreciated!
#include <stdio.h>
#include <string.h>
#define SIZE 10
int findTarget(char *target, char nameptr[SIZE][80], int size);
int main()
{
char nameptr[SIZE][80];
char t[40];
int i, result, size;
printf("Enter no. of names: ");
scanf_s("%d", &size);
printf("Enter %d names: ", size);
for (i = 0; i < size; i++)
scanf_s("%s", &nameptr[i]);
printf("Enter target name: ");
scanf_s("\n");
gets(t);
result = findTarget(t, nameptr, size);
printf("findTarget(): %d\n", result);
return 0;
}
int findTarget(char *target, char nameptr[SIZE][80], int size)
{
int i;
for (i = 0; i<size; i++) {
if (strcmp(nameptr[i], target) == 0)
return i;
}
return -1;
}
This:
scanf_s("%s", &nameptr[i]);
should be
scanf_s("%s", nameptr[i], sizeof(nameptr[i]));
/* Or better */
scanf_s("%79s", nameptr[i], sizeof(nameptr[i]));
or
scanf_s("%s", nameptr[i], _countof(nameptr[i]));
/* Or better */
scanf_s("%79s", nameptr[i], _countof(nameptr[i]));
because the %s in scanf_s expects a third argument denoting the maximum size of the string argument used. More information on this can be found at the msdn documentation of scanf_s

printing a 2d array of string in c

i'm trying to print a 2d array of string as practice(i'm a newbie) with no success i've tried every combination i could think of still nothing i'm sure i'm doing a silly error somewhere i just can't see it here some of the example:
using a pointer :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define lim 10
#define maxx 25
void print(char *);
int main()
{
int i = 1;
char input[lim][maxx];
char *ps = input;
printf("type the list of %d names or type quit to leave \n", lim);
while (i<lim && gets(input[i]) != NULL && strncmp(input[i], "quit", 4)!=0 ) {
i++;
}
printf("i've counted %d names\n", i);
print("\n");
print(ps);
return 0;
}
void print(char *a)
{
int i=0;
printf("the list of names include : \n");
while(*(a) != '\0') {
printf("%s\n", *(a+i));
i++;
}
}
here's the output:
type a list of %d names or type quit to leave :
bla
bli
blo
quit
i've counted 4 names
the list of names include :
segmentation fault (core duped)
another version of the print function is like this :
void print(char aray[lim][maxx])
{
int i,j;
printf("the list of names include : \n");
for(i = 0; i < lim; i++) {
for(j = 0; j < maxx; j++){
puts(aray[i][j]);
//printf("%s\n", aray[i][j]);
}
}
}
i get the same output, can anyone help me debug this ? and thx in advance
In short, it looks like you need to brush up on your pointers. With your original print function:
void print(char *a)
{
int i=0;
printf("the list of names include : \n");
while(*(a) != '\0') {
printf("%s\n", *(a+i));
i++;
}
}
You are printing the value at a + i every iteration. This might sound like what you want, but what you actually pass to print is a pointer to an array of arrays of char (your compiler should be throwing a warning about incompatible pointer types). That is, the "proper" type of ps is (char *)[]. So in the print function you are only advancing the memory address by sizeof(char) with each iteration, whereas what you actually want is to increment it by sizeof(char) * maxx (the size of your array entries). To implement this change, do the following:
change declaration of print
void print(char (*)[maxx]);
change to proper pointer type
char (*ps)[maxx] = input;
And finally, change print function to something like:
void print(char (*a)[maxx]){
printf("the list of names include : \n");
int i;
for (i = 0; i < lim; i++){
printf("%s\n",*a);
a++;
}
}
You need not use the (a+i) syntax, as just advancing a by one each iteration accomplishes the same thing, and is possibly faster for large i. And of course, as others have mentioned, double check your new line printing, I believe you want printf('\n').
You are adding i as 1 which will not help in case of your two dimensional array as the next element will be at maxx location,so you can do something like this
//here lim and max are defined in your program
void print(char *a){
int i=0;
printf("the list of names include : \n");
while(i<(lim*maxx)){
printf("%s\n",a );
i += maxx;
a = a + maxx;
}
}
and the second variant should be
void print(char aray[lim][maxx])
{
int i,j;
printf("the list of names include : \n");
for(i = 0; i < lim; i++) {
cout<<aray[i]<<"\n";
}
}
You start on index 1 in your 2d array, you should start with index 0
int i=1;
Your print function takes an array of characters and then does a printf string of each character which makes no sense
void print(char *a)
{
int i=0;
printf("the list of names include : \n");
while(*(a)!='\0')
{
printf("%s\n",*(a+i));
i++;
}
}
instead make it look like this
void print(char *a[], int strings)
{
int i = 0;
for (; i < strings; ++i)
{
puts( a[i] );
}
}
and call it with the number of strings you read
print(ps,i);
You would also be better off using fgets() instead of gets(), especially since your strings are max 25 chars so its easy to give a longer string. fgets() lets you specify the max size of the string fgets(input[i],maxx,stdin)
Your other function
void print(char aray[lim][maxx])
{
int i,j;
printf("the list of names include : \n");
for(i = 0; i < lim; i++) {
for(j = 0; j < maxx; j++){
puts(aray[i][j]);
//printf("%s\n", aray[i][j]);
}
}
}
does a similar wrong assumption about the level of indirection
arra[i][j] is one character but puts takes a string argument, so puts( arra[i][j] ); is not correct, you could try fputc( arra[i][j], stdout ) instead since fputc takes one character
fix to
void print(char (*)[maxx]);
int main()
{
int i = 0;//int i = 1;
char input[lim][maxx] = { {'\0'}};
char (*ps)[maxx] = input;
printf("type the list of %d names or type quit to leave \n", lim);
while (i<lim && gets(input[i]) != NULL && strncmp(input[i], "quit", 4)!=0 ) {
i++;
}
printf("i've counted %d names\n", i);
printf("\n");//print("\n");
print(ps);
return 0;
}
void print(char (*a)[maxx])
{
int i=0;
printf("the list of names include : \n");
while(i<lim && a[i][0] != '\0') {
printf("%s\n", a[i]);
i++;
}
}

For loop trouble across compilers

I am having trouble. My program seems to work fine on DEV C++,but on Xcode the last For loop doesn't know when to stop. any help?
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
void strip_newline(char *str,int size)
{
int i;
for(i=0;i<size;++i)
{
if(str[i]=='\n')
{
str[i]='\0';
return;
}
}
}
int main()
{
int randomnumber;
int max;
int tall;
char name[40][tall];
char seat[40][tall];
int count;
int currentcount;
int flag;
srand( time(NULL) );
printf("Enter total number of students: ");
scanf("%d",&max);
getchar();
tall=max+1;
randomnumber=rand()% max +1;
printf("This is your random number\n %d \n",randomnumber);
printf("Enter your students names and press enter after each name:\n ");
fgets(name[0],40,stdin);
strip_newline(name[0],40);
for(count=1; count < max; count++ )
{
printf("Please enter next name\n ");
fgets(name[count],40,stdin);
strip_newline(name[count],40);
}
count=-1;
do {
randomnumber=rand()% max;
flag=0;
for(currentcount=0; currentcount<max; currentcount++)
{
if(strcmp(name[randomnumber],seat[currentcount])==0)
{
flag=1;
}
else
{
}
}
if(flag==0)
{
strcpy(seat[count],name[randomnumber]);
count++;
}
else
{
}
}
while (count != max);
for(count=0; count < max; count++)
{
printf("%s sits in seat %d\n",seat[count],count+1);
}
getchar();
return 0;
}
Your problem is in these lines:
int tall;
char name[40][tall];
char seat[40][tall];
as tall is not initialized (not given a value), it is unknown how large your 40 arrays will become. It could be anything between 0 and a very large number, or even 'blow-up-in-your-face' as the behaviour is formally undefined. The later assignment to tall will not magically resize the arrays for you.
The solution is to re-arrange your code such that the arrays are not declared until you have sufficient information about their size. Also, given how you use them, you seem to want tall arrays of 40 characters, not 40 arrays of tall characters, so you need to swap the dimensions:
//...
printf("Enter total number of students: ");
scanf("%d",&max);
getchar();
tall=max+1;
char name[tall][40];
char seat[tall][40];
//...

Resources