Problems with '\0' in 2d array - c

For some reason, when I execute case 1, I am assigned the time 4pm, so it doesn't find the \0 in the first 3 instances of i, but finds it on the fourth. I am just a little confused on how the null character works with a 2d array, is it stored by default in every row, does it need to be added? I am trying to have a 5x20 array with 5 different slots for names. Thanks in advance
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int i;
int j;
int total=0;
int opt;
int time;
char x[5][20];
char name[20];
printf("----MAIN MENU----\n");
printf("1: Request a lesson\n");
printf("2: Cancel a lesson\n");
printf("3: See available lessons\n");
printf("4: List all names starting with a letter\n");
printf("9: Quit\n");
for(;;)
{
scanf("%d",&opt);
switch(opt)
{
case 1:
printf("please enter your name\n");
scanf("%s",name);
//if schedule is full
if(total==5)
{
printf("Sorry, the teacher is too busy, try again tomorrow.\n");
}
//opening in schedule
else
for(i=0;i<5;i++)
if (x[i][0]=='\0')
{
strcpy(x[i], name);
total++;
printf("You have been assigned the time %dpm\n",i+1);
break;
}
break;

Jeff Mercado is correct, op. You will have undefined behavior if you don't initialize those arrays. I suggest you insert the nul character '\0' on every first position, to make it coherent with the code you had already done. You could do that on your main function:
for (i = 0; i < 5; i++) x[i][0] = '\0';

Related

Edit my struct in C with pointer

I want to create an edit function that receives as a parameter by reference the vector of songs. (using pointers)
The user must choose the song number and re-enter the data of that position of the vector.
I created the struct, I am already receiving the values and I am playing. But I do not know how to edit. Anyone to help me start this part?
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <locale.h>
#include <string.h>
struct registry_of_music {
char name[50];
char artist[60];
char url[80];
};
struct registry_of_music music[9];
int main() {
int i;
printf("\nRegistry of Music\n\n\n");
for(i = 0; i <= 3;i++ ){
printf("Name of Music: ");
fflush(stdin);
fgets(music[i].name, 50, stdin);
printf("Name of Artist: ");
fflush(stdin);
fgets(music[i].artist, 60, stdin);
printf("URL of Internet: ");
fflush(stdin);
fgets(music[i].url, 80, stdin);
}
int op;
do
{
printf("1 - Play\n");
printf("2 - Edit\n");
printf("3 - Exit\n");
printf("Please enter a value:");
scanf("%d", &op);
switch(op) {
case 1: play();
break;
case 2: edit();
break;
case 3: printf("Bye\n");
break;
default: printf("Try Again\n");
}
} while (op!=3);
getch();
return(0);
}
void play(){
int i;
for(i = 0; i <= 3;i++ ){
printf("Name ...........: %s", music[i].name);
printf("Artist .....: %s", music[i].artist);
printf("URL .....: %s", music[i].url);
}
}
void edit(){}
The «fill instance of structure» action is absolutely identical if performing on uninitialized structure or initialized. Even if an instance is not initialized, it has some rubbish values in its fields.
On the other hand there is no way to specify default value which will be shown in fgets's prompt and will be available for keyboard edit, unless you're using much more complicated (and NOT included in ISO C standard) tools.

Find Biggest Number in C, by N number of inputs

So I have this code:
#include <stdio.h>
int main()
{
char peopleName[5][20],peopleAge[5];
int i;
int maxAge=0, maxName=-1;
for(i=0;i<5;i++)
{
printf("Name & Age %d :",i+1);
scanf("%s",&peopleName[i]);
scanf("%d",&peopleAge[i]);
if(peopleAge[i]>maxAge)
{
maxAge=peopleAge[i];
maxName=i;
}
}
printf("%s %d", peopleName[maxName],peopleAge[maxAge]);
}
This code finds from 5 people the oldest one. I want to change from 5 people to N number of people, whatever the number I input myself. (For example I put 7, and I can insert seven names and ages and so on).
The question has two parts: How does the user specify how many persons are entered? And how do I store the data?
The second part is easy: No matter how many persons you are going to consider, if you just want to know who is the oldest, it is enough to keep the name and age of the currently oldest person. (Of course, if there is a tie and many people are, say, 80 years old, you just get to keep the first match.)
Not storing anything also simplifies the first question. You could ask the user to specify the number of persons beforehand and that's find if you have few people. If you have a list of many people, the user would have to count the by hand and then enter the count. Miscounting is very likely.
A better way is to indicate the end of input by another means, for example by a negative age or by two dashes as name. There is also the possibility that the input runs out, for example when redirecting input from a file or when pressing one of Ctrl-Z or Ctrl-D, depending on your platform, after the input.
The example below read the input line-wise and then scans that line. The loop while (1) is in theory infinite, in practice execution breaks out of the loop when the input runs out – fgets return NULL –, when a blank line is read or when the input isn't in the format single-word name and age.
#include <stdio.h>
int main(void)
{
char oldest[80] = "no-one";
int max_age = -1;
int count = 0;
puts("Enter name & age on each line, blank line to stop:");
while (1) {
char line[80];
char name[80];
int age;
if (fgets(line, sizeof(line), stdin) == NULL) break;
if (sscanf(line, "%s %d", name, &age) < 2) break;
if (age > max_age) {
strcpy(oldest, name);
max_age = age;
}
count++;
}
printf("The oldest of these %d people is %s, aged %d.\n",
count, oldest, max_age);
return 0;
}
You can do this -
int n; // number of people
scanf("%d",&n); // take input from user
char peopleName[n][20],peopleAge[n]; // declare 2-d array
for(i=0;i<n;i++)
{
// your code
}
Also this statement -
scanf("%s",&peopleName[i]); // pass char * as argument to %s
should be -
scanf("%19s",peopleName[i]); // one space is left for null character
You can use malloc to allocate buffer dynamically.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char (*peopleName)[20];
int *peopleAge;
int i;
int maxAge=0, maxName=-1;
int dataNum;
printf("How many people? :");
if(scanf("%d",&dataNum)!=1)return 1;
peopleName=malloc(sizeof(char[20])*dataNum);
peopleAge=malloc(sizeof(int)*dataNum);
for(i=0;i<dataNum;i++)
{
printf("Name & Age %d :",i+1);
scanf("%s",peopleName[i]);
scanf("%d",&peopleAge[i]);
if(peopleAge[i]>maxAge)
{
maxAge=peopleAge[i];
maxName=i;
}
}
printf("%s %d", peopleName[maxName],peopleAge[maxName]);
free(peopleName);
free(peopleAge);
return 0;
}
Also please note that:
You should pass char*, not char(*)[20], for %s in scanf
peopleAge[maxAge] may be out of bounds. maxName (or other name but same role) should suit here.

why my program stops working when type enter?

My main goal for this code is to capture the users input and do whatever he wants to do with the choices I have presented, but I'm stuck: when I compile, I can only type the word and the program stops working.
i have no idea where I'm making a mistake.
The is my code:
#include <stdio.h>
#include <string.h>
#define MAX_STRING_LENGTH 100
void grab_user_input(void);
void load_menu(void);
void Count_the_letters(void);
int main(void)
{
grab_user_input();
return 0;
}
void grab_user_input(void)
{
char word;
{
printf("Please enter a single word (25 characters or less): \n");
scanf("%s", &word);
printf("Thanks! The word you entered is: %s\n", word);
}
void load_menu(void)
{
int choice;
do
{
int choice;
printf("\n(:===Menu====:)\n");
printf("1. Count_the_letters\n");
printf("2. Count_the_vowels\n");
printf("3. Reverse_the_word\n");
printf("4. Check_if_palindrome\n");
printf("5. Enter_a_new_word\n");
printf("6. Exit\n");
scanf("%d", &choice);
switch (choice)
{
case 1: Count_the_letters();
break;
}
} while (choice != 3);
}
void Count_the_letters(void)
{
char S[MAX_STRING_LENGTH];
int count;
count = 0;
do {
printf("string:\t");
scanf("%s",S);
if (strcmp(S,"exit") != 0)
++count;
} while (strcmp(S,"exit") != 0);
printf("word count:\t%d\n", count);
}
return 0;
}
scanf("%s", &word);
needs an array of characters to read the data. &word only has space for one character.
You are running into undefined behavior.
Use
char word[26];
scanf("%25s", &word);
The reason is that you are passing the address to the char variable you declared and scanf() is trying to write two bytes where it only fits one.
char word
this declares a char variable, it can hold a single byte
scanf("%s", &word);
whill require at least one byte for an empty string the '\0'.
But also, you declared a lot of functions inside void grab_user_input(void), that is not valid standard c, it might work with some compiler, but it's not standard.

How to get rid of the space/ enter of the input buffer in vim?

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct st{
char n[100]; //Name
char d[100]; //lastname
} arr[4];
void add(int *c, struct st l[])
{
int i =*c;
int arrSize =sizeof(arr) / sizeof(arr[0]);
if((*c)<arrSize)
{
printf("Enter a name :\n");
fgets(l[i].n, 100,stdin);
printf("Enter lastname :\n");
fgets(l[i].d,100,stdin);
printf(" SUCCESS. Person was added\n");
}
}
int main(void)
{
int ct =0;
int *ctPointer=&ct;
char response ;
char endWhileloop =0;
while(endWhileloop==0)
{
printf("To add a person to the list hit 'a' \n");
printf("to end program enter 'q'\n");
fgets(&response,2,stdin);
fseek(stdin,0,SEEK_END);
switch(response)
{
case 'a':
add(&ct, arr);
break;
case 'Q':
endWhileloop=1;
break;
}
}
printf("\nbye.");
return 0;
}
I am trying to run my code in an older version of Vim(maybe an older version of C) for my school. Unfortunately I am not certain what version they are running
Surprisingly, my code works from home using vim and eclipse. but not from school
:I tried---> fgets, scanf("%[^\n]s",name) , scan( %c,&name), fseek(stdin,0,SEEK_END),flush(stdin);
But nothing has worked for me. I would like to know of some possible solutions.
When I run my code from school(not home), after I enter 'a' my code prints: Enter name..(line in between) Enter last name.
Without taking an input.
Place this line instead of fseek
After getting the option you are not clearing the input buffer. so this is the reason for the not getting the first input.
After entering the first input new line will be there in input buffer. After processing first input then buffer will give the \n.
So place this line after getting the option. Declare the variable int c;
while((c=getchar()) != '\n' && c!= EOF );
Then make the case into like this,
case 'a': case 'A':
...
...
case 'q': case 'Q':
...
...
For getting the option you can use simply the scanf like this.
scanf(" %c",&response);

array changes when i change parameter

So i have this code in c.It all works fine until i get to the point to read word again.It gets the new word but also the (*A)[size-1] takes the price of the new word.How do i prevent this?
void fuction(char ***A,char ***B,int size)
{
char word[20],word2[20];
printf("Type word .\n");
gets(word);
while(strcmp(word,"0")!=0)
{
printf("Type second word.\n");
gets(word2);
printf("%d",size);
**A=realloc(**A,(size+1)*sizeof(char));
**B=realloc(**B,(size+1)*sizeof(char));
(*A)[size-1]=word;
(*B)[size-1]=word2;
size++;
printf("Type another word to add or 0 to exit.\n");//**it all works fine**
gets(word);
}
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void function(char ***A, char ***B, int *size){
char word[32], word2[32];
printf("Type first word.\n");
scanf("%31s", word);
while(strcmp(word,"0")!=0){
printf("Type second word.\n");
scanf("%31s", word2);
*A =realloc(*A, (*size+1)*sizeof(char*));
*B =realloc(*B, (*size+1)*sizeof(char*));
(*A)[*size]=strdup(word);
(*B)[*size]=strdup(word2);
++*size;
printf("Type another word to add or 0 to exit.\n");
scanf("%31s", word);
}
}
int main(void){
int i, size = 0;
char **w1, **w2;
w1 = w2 = NULL;
function(&w1, &w2, &size);
for(i = 0; i < size; ++i){
printf("%s, %s\n", w1[i], w2[i]);
free(w1[i]);free(w2[i]);
}
free(w1);free(w2);
return 0;
}
Turns out the problem was that i didnt allocate memory for the words in the array.I added these lines and it worked.Thank you for your answers.
(*A)[size-1]=(char*) malloc(31);
(*B)[size-1]=(char*) malloc(31);
This
(*A)[size-1]=word;
(*B)[size-1]=word2;
is not what you think it is.
In c, this means you are assigning the address to the first element of the array word to (*A)[size-1] if you want this to work, provided that you have allocated memory for (*A)[size-1] you should do it this way
strcpy((*A)[size-1], word);
strcpy((*B)[size-1], word2);
You should think about why do you need char ***, generally you wont need more than char **, and don't use gets() use fgets() instead.

Resources