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];
//...
Related
I am new to this plz can anyone help me to correct this program.
This is a program to print the entered name nth times.
#include <stdio.h>
char* call(int i, int n,char name[30]){
if (i<=n)
return char name[30] ;
}
int main() {
int i, n;
char name[30];
printf("Enter name: ");
fgets(name, sizeof(name), stdin);
printf("How many time do you want to print: ");
scanf("%d", &n);
for (i=1; i<=n; ++i)
{
printf("%s\n",call(i,n,name[30]));
}
return 0;
}
Recursion is a way to use loops but it costs memory on stack due to function calls. And always a recursive function have a condition to exit the calls, in the case is when the i reaches the value of n.
#include <stdio.h>
void call(int i,char name[30]){
if(i==0)
return;
printf("%s\n",name);
call(i-1,name);
}
int main() {
int n;
char name[30];
printf("Enter name: ");
fgets(name, sizeof(name), stdin);
printf("How many time do you want to print: ");
scanf("%d", &n);
call(n,name);
return 0;
}
If you want to do recursion, you don't want a for loop. The number of recursive calls represents the loop.
Try like:
#include <stdio.h>
void call(int n, const char* name)
{
if (n <= 0) return;
if (n > 1) call(n-1 , name);
printf("%s\n", name);
}
int main()
{
int n = 5;
char name[30] = "Hello";
call(n, name);
return 0;
}
using namespace std;
int main() {
int input;
int i=0;
while (1){
scanf("%d", &input);
printf("%d input:%d\n", i, input);
i++;
}
}
Stdin Inputs:
10
65
100
30
95
.
.
.
Is there a way to stop the code and escape from while loop after hitting the last Input?
Amount of Inputs can be N.
edition) Is there a way to calculate the amount of Stdin Inputs? This is my major question.
using namespace std;
int main() {
int input;
int i=0;
while (1){
scanf("%d", &input);
printf("%d input:%d\n", i, input);
i++;
if (i >= 5) break; //change 5 to your desired amount of inputs
}
}
Maybe you are reading an input that has an EOF (End of file)?
In that case you should stop when receiving EOF
#include <stdio.h>
#include <ctype.h>
int main(void)
{
int input;
int i = 0;
while (scanf("%d", &input) != EOF){
printf("%d input:%d\n", i, input);
i++;
}
printf("End\n");
return 0;
}
Otherwise you can do a program that first reads N and then iterate N time
int main(void)
{
int input;
int i = 0;
int n = 0;
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d", &input);
printf("%d input:%d\n", i, input);
}
return 0;
}
I haven't been coding for long (just 2 months).
My question has to do with the iteration of the counter in loops. Below is my program
with while:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int cnt=0;
int match;
int position;
int tries=0;
char letter;
int main()
{FILE *Difficult= fopen("/GODlvl.txt", "r");
char singl[19];
if (Difficult==NULL){
return -1;}
for(int i = 0; i < rnd1; i++)
fgets(singl, 21, Difficult);
int DashN1= strlen(singl);
printf("%s", singl);
for (int i=0; i<DashN1-1; i++)
{
singl[i]='_';
singl[DashN1]='\0';
}
for (int i=0; i<DashN1; i++) /**it adds an extra character ..possibly a space..**/
{
printf(" ");
printf(" %c", singl[i]);
}
do{
scanf(" %c", &letter);
if(letter==singl[cnt])
{
match=1;
position=cnt;
printf("match found");
}
if (position==cnt)
{
singl[position]=letter;
printf(" %s", singl);
}
cnt++;
tries++;
}
while(tries!=8);
}
the do loop runs starting from 0, and iterates after every step. The problem with this is with the if conditions; they don't test for any arbitrary element in the char array (singl). How can i edit this code(whether the if conditions or the loop) to run for arbitrary index.
After reading the user input for letter, you can request a random index for use in singl, just by doing:
srand((unsigned)time(NULL));
cnt = rand() % DashN1;
This means that your do loop, should look like:
do{
scanf(" %c", &letter);
srand((unsigned)time(NULL));
cnt = rand() % DashN1;
if(letter==singl[cnt]){
//....
}
}while(/*...*/);
Make sure that you do:
#include<stdlib.h>
in order to access srand and rand
This is what I theorized it should be but it seemed like it doesn't work.
HELP PLEAZEE
int main()
{
char input[50];
int i;
do{
printf("ENTER A CHARACTER:");
scanf("%s",&input);
if(isalpha(input)!=0){
printf("YOU INPUTTED A CHARACTER");
i++;
}else{
printf("INVALID INPUT\n");
}
}while(i!=1);
}
isalpha takes an integer as an argument.
You are giving a char array.
You should loop for the number of characters given in input[], if you want more than one character (hard to tell from this code).
This exits if you give exactly one character but keeps going if you give more than one:
#include <stdio.h>
#include <string.h>
int main()
{
char input[50];
int i = 0, j;
size_t len = 0;
do
{
printf("ENTER A CHARACTER:");
scanf("%s",&input);
len = strlen(input);
for(j = 0; j < len; j++) {
if(isalpha(input[j]))
{
i++;
printf("YOU INPUTTED A CHARACTER %d\n", i);
}
else
{
printf("INVALID INPUT\n");
break;
}
}
} while(i!=1);
}
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");
}