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
Related
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 want to have unknown amount of inputs in a single line. For example, user can input:
"ans: 1 2 3 4 5"
and scanf() will store these five numbers to an array. The problem is that the program don't know how many input will there be.
#include <stdio.h>
int main()
{
int i;
int input[4];
scanf("ans: " for(i = 0, i < 3,i++){scanf(" %d", &input[i]);};
return 0;
}
Sorry I'am totally new to coding, what will be the proper way to write this? Or is this impossible?
Thanks :)
Use fgets() and sscanf() with "%n"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
char input[100];
int arr[10];
//fgets(input, sizeof input, stdin);
strcpy(input, "1 2 42 56 -3 0 2018\n"); // fgets
char *pi = input;
int tmp, pp, i = 0;
while (sscanf(pi, "%d%n", &tmp, &pp) == 1) {
if (i == 10) { fprintf(stderr, "array too small\n"); exit(EXIT_FAILURE); }
pi += pp;
arr[i++] = tmp;
}
printf("got this ==>");
for (int k = 0; k < i; k++) printf(" %d", arr[k]);
puts("");
}
You asked this question way round.
You can achieve what you expect by putting scanf inside of a loop.Even you can ask user to give how many inputs he want to enter.
#include <stdio.h>
int main()
{
int i;
int input[4];
printf("Enter the number of inputs you want to give : ");
scanf("%d", &n);
for(i = 0; i < n;i++)
{
printf("Enter the input number %d : ",i);
scanf("%d", &input[i]);
}
return 0;
}
I've printed a string of "+" symbols based on two given values(N, M). Now I'm trying to figure out how to replace characters at random in said string based on a third given value(K). The characters are stored in a string(l). I think I have to use the replace function but I don't know how(hence why it's in a comment for now). Any help is appreciated.
#include <stdio.h>
unsigned int randaux()
{
static long seed=1;
return(((seed = seed * 214013L + 2531011L) >> 16) & 0x7fff);
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
char s[1000];
int N, M, K, l;
printf("N: ");
scanf("%d",&N);
printf("M: ");
scanf("%d",&M);
printf("K: ");
scanf("%d",&K);
printf("\n");
gets(s);
l=strlen(s);
/* Mostre um tabuleiro de N linhas e M colunas */
if(N*M<K){
printf("Not enough room.");
}else if(N>40){
printf("Min size 1, max size 40.");
}else if(M>40){
printf("Min size 1, max size 40.");
}else{
for(int i=0; i<N; i++)
{
for(int j=0; j<M; j++)
{
printf("+", s[j]);
}
printf("\n", s[i]);
}
for(int l=0; l<K; l++)
{
/*s.replace();*/
}
}
return 0;
}
There is too much unexplained complexity and unknowns in your program to enable a corrective answer. But this shows how to replace a textual string's character at random, with a numeral.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int main(void)
{
char str[] = "----------";
int len = strlen(str);
int index;
int num;
srand((unsigned)time(NULL)); // randomise once only in the program
printf("%s\n", str); // original string
index = rand() % len; // get random index to replace, in length range
num = '0' + rand() % 10; // get random number, in decimal digit range
str[index] = num; // overwrite string character
printf("%s\n", str); // altered string
return 0;
}
Program sessions:
----------
-3--------
----------
-----0----
----------
--------6-
Arguably it would be better to use size_t types, but for the limited range of the example, will suffice.
I want to make a program that counts the vowels in a sentence entered by the user.
For that compare a character that capitalizes with the vowel arrangement, but although they do not pulls error, do not type the correct output.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <conio.h>
//Program EJER004
int main(){
char vowels[5] = {'A','E','I','O','U'};
int count;
char letter;
count = 0;
printf("Enter a phrase, ending with a point\n");
do{
letter=getchar();
if (toupper(letter) == vowels[5]) /*attempt ask if is a vowel the letter introduced*/
count++;
}while (letter != '.');
printf("\n");
printf("\n");
printf("The number of vowels in the phrase introduced is% d", count);
getch();
return 0;
}
It think the problem is the comparison toupper(letter) == vowels[5]? vowels[5] is always out of array, but other vowels aren't checked.
You would need to add a loop like:
char upr=toupper(letter);
for(int i=0; i<5; i++)
if(vowels[i]==c)
{ cont++;
break;
}
You should write a small function for this comparison
int checkforVowels(char tobechecked)
{
//this only get vowels in CAPSLOCK tho... so dont forget toupper
char vowels[5] = {'A','E','I','O','U'};
int hasvowel = 0;
for(int i = 0; i < 5; i++)
{
if(tobechecked == vowels[i])
{
hasvowel = 1;
break;
}
}
return hasvowel;
}
so you can have it like that
if(checkforVowels(toupper(letter))
HTH
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];
//...