I want to ask you guys, here my code cannot check is my value is exist or not in struct, I've input the value, but none of them enter the if else condition, can anyone help me?
#include <stdio.h>
int main(){
int a,i;
struct data {
char nim[10];
};
struct data batas[100];
printf("TEST1 : "); scanf("%[^\n]s", batas[0].nim);
printf("TEST2 : "); scanf(" %[^\n]s", batas[1].nim);
printf("TEST3 : "); scanf(" %[^\n]s", batas[3].nim);
printf("TEST : "); scanf(" %[^\n]s", batas[a].nim);
for(i=0; i<a; i++){
if (batas[a].nim == batas[i].nim) {
printf("Value exist");
} else {
printf("Value doesn't exist");
}
}
return 0;
}
You can not compare array of chars with the equal operator, instead:
if (strcmp(batas[a].nim, batas[i].nim) == 0)
or
if (!strcmp(batas[a].nim, batas[i].nim))
you need to #include <string.h>
Also, note that you are using a uninitialized.
from what you give, it still can't enter the "Value exist" value. Here are my full line of code.
#include <stdio.h>
#include <string.h>
struct data {
char nim[10];
};
struct data batas[100];
int a=0, b, c, d;
int i, j;
char x[20];
void inputdata()
{
printf("\nInput Data\n");
printf("=======================\n");
printf("NIM : "); scanf("%s", batas[a].nim);
for(i=0; i<a; i++){
if (!strcmp(batas[a].nim, batas[i].nim)) {
strcpy(x, "FLAG");
} else {
strcpy(x, "FLAGX");
}
}
printf("%s", x);
a++;
}
void showdata()
{
j=0;
for(i=0; i<a; i++){
j = j + 1;
printf("\nData-%i", j);
printf("\nNIM : %s", batas[i].nim);
}
}
int main() {
int menu;
do {
printf("\nChoose input = "); scanf("%d", &menu);
switch(menu)
{
case 1 : inputdata(); break;
case 2 : showdata(); break;
}
}while (menu != 3);
return 0;
}
Related
The code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 500
#define M 100
/*Version 1*/
void replace_word(char *word);
void add_new_word_dictionary(void);
void do_nothing(void);
void return_basic(void);
void check_word(void);
void compute_words(void);
void compute_characters(void);
void compute_ch_sp(void);
void compute_num_dif_words(void);
void create_istogram(void);
void save_file(void);
int get_choice(void);
void return_word(void);
void insert_text(int numwords, char matrix[N][M], int posit);
int main() {
int j;
int choice = 0;
char matrix[N][M];
char word[40] = { "t" };
while (1) {
choice = get_choice();
if (choice == 0) {
insert_text(M, matrix, 1);
}
if (choice == 1) {
add_new_word_dictionary();
}
if (choice == 2) {
do_nothing();
}
if (choice == 3) {
save_file();
}
if (choice == 4) {
compute_words();
}
if (choice == 5) {
break;
}
for (j = 0; j < M; j++) {
printf("%s", matrix[N][M]);
}
}
printf("\n End of Program \n");
return 0;
}
void replace_word(char *word) {
return;
}
void add_new_word_dictionary(void) {
char word[50] = { "s" };
printf("\nPlease enter the word\n");
scanf("\n%s", word);
printf("Your word is %s", word);
return;
}
void do_nothing(void) {
printf("\n do_nothing \n");
return;
}
void return_basic(void) {
printf("\n return basic \n");
return;
}
void check_word(void) {
printf("\n check word \n");
return;
}
void compute_words(void) {
printf("\n compute_words \n");
return;
}
void compute_characters(void) {
printf("\n compute characters \n");
}
void compute_ch_sp(void) {
printf("\n compute_ch_sp \n");
return;
}
void compute_num_dif_words(void) {
printf("\n compute_num_same_words \n");
return;
}
void create_istogram(void) {
printf("\n create istogram \n");
return;
}
void save_file(void) {
printf("\n save_file \n");
return;
}
int get_choice(void) {
int choice = 0;
printf("\n Select a choice from the below \n");
printf("\n Select 0 to add text \n");
printf("\n Select 1 to add new words in the dictionary \n");
printf("\n Select 2 to enter enter correction mode \n");
printf("\n Select 3 to save the text \n");
printf("\n Select 4 to see the statistics about your text \n");
printf("\n Select 5 to exit the program\n");
scanf("\n%d", &choice);
return choice;
}
void insert_text(int numwords, char matrix[N][M], int posit) {
int i;
int j;
char word2[40] = { "" };
while (strcmp(word2, "*T*E*L*O*S*")) {
printf("\n Add the word \n");
scanf("\n%s", word2);
if (posit + 1 > numwords) {
printf("\n Out of Bounds \n ");
}
for (i = numwords - 2; i >= posit; i--) {
strcpy(matrix[i + 1], matrix[i]);
if (!i)
break;
}
strcpy(matrix[posit], word2);
j++;
}
for (i = 0; i < j; i++) {
printf("%s", matrix[i]);
printf("\n j is %d\n", j);
}
return;
}
The problem: I have a function called insert_text. This function adds a string in the 1st position of an array (at least that is what I think it does) and it is called if choice is 0 and executes itself until the string *Ī¤ELOS* is given. When in insert_text I print matrix I get a bunch of *(null)*s... I can count how many words matrix has (by declaring a variable j and incrementing by 1 inside the while loop, but that does not seem to work either. How can I fix this?
The printing code is incorrect: matrix is an array of N arrays of M characters, where you store null terminated C strings. As coded, you pass a single character just beyond the end of the array to printf for %s, which expects a string. The loop should be:
for (j = 0; j < N; j++) {
printf("%s ", matrix[j]);
}
Note that char matrix[N][M]; is uninitialized, so its contents will seem random. Initialize matrix as char matrix[N][M] = { "" };
Also note that in add_new_word_dictionary(), the scanf() conversion should be scanf("%49s", word); to prevent a potential buffer overflow if the user enters a very long word.
Same in insert_text, the code should be scanf("%39s", word2) ans you should test the return value to check for input errors.
Finally, arrays are indexed from 0 in C, so insert_text should be given a position of 0 and the number of words should be N, not M.
Both the test and the insertion loop have problems too.
Here is a modified version:
// call from main as insert_text(N, matrix, 0);
//
void insert_text(int numwords, char matrix[N][M], int posit) {
char word2[40];
int i, j = 0;
for (;;) {
printf("\n Add the word \n");
if (scanf("%39s", word2) != 1) {
break; // end of file or input error
}
if (!strcmp(word2, "TELOS")) {
break; // magic word
}
if (posit >= numwords) {
printf("\n Out of Bounds \n");
break;
}
for (i = numwords - 2; i >= posit; i--) {
strcpy(matrix[i + 1], matrix[i]);
}
strcpy(matrix[posit], word2);
j++;
}
for (i = 0; i < j; i++) {
printf("%s ", matrix[i]);
}
printf("\n j is %d\n", j);
}
Here is a modified version of the whole program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 500
#define M 100
/*Version 1*/
void replace_word(char *word);
void add_new_word_dictionary(void);
void do_nothing(void);
void return_basic(void);
void check_word(void);
void compute_words(void);
void compute_characters(void);
void compute_ch_sp(void);
void compute_num_dif_words(void);
void create_istogram(void);
void save_file(void);
int get_choice(void);
void return_word(void);
void insert_text(int numwords, char matrix[N][M], int posit);
int main() {
int j, done = 0;
char matrix[N][M] = { "" };
while (!done) {
switch (get_choice()) {
case 0:
insert_text(N, matrix, 0);
break;
case 1:
add_new_word_dictionary();
break;
case 2:
do_nothing();
break;
case 3:
save_file();
break;
case 4:
compute_words();
break;
default:
done = 1;
break;
}
for (j = 0; j < N; j++) {
if (matrix[j][0])
printf("%s ", matrix[j]);
}
printf("\n");
}
printf("\n End of Program \n");
return 0;
}
void add_new_word_dictionary(void) {
char word[50];
printf("\nPlease enter the word\n");
if (scanf("%49s", word) == 1)
printf("Your word is %s\n", word);
}
void replace_word(char *word) { printf("\n replace word \n"); }
void do_nothing(void) { printf("\n do_nothing \n"); }
void return_basic(void) { printf("\n return basic \n"); }
void check_word(void) { printf("\n check word \n"); }
void compute_words(void) { printf("\n compute_words \n"); }
void compute_characters(void) { printf("\n compute characters \n"); }
void compute_ch_sp(void) { printf("\n compute_ch_sp \n"); }
void compute_num_dif_words(void) { printf("\n compute_num_same_words \n"); }
void create_istogram(void) { printf("\n create istogram \n"); }
void save_file(void) { printf("\n save_file \n"); }
int get_choice(void) {
int choice = -1;
printf("\nSelect a choice from the below \n");
printf("Select 0 to add text \n");
printf("Select 1 to add new words in the dictionary \n");
printf("Select 2 to enter enter correction mode \n");
printf("Select 3 to save the text \n");
printf("Select 4 to see the statistics about your text \n");
printf("Select 5 to exit the program\n");
scanf("%d", &choice);
return choice;
}
// call from main as insert_text(N, matrix, 0);
//
void insert_text(int numwords, char matrix[N][M], int posit) {
char word2[40];
int i;
for (;;) {
printf("\n Add the word \n");
if (scanf("%39s", word2) != 1) {
break; // end of file or input error
}
if (!strcmp(word2, "TELOS")) {
break; // magic word
}
if (posit >= numwords) {
printf("\n Out of Bounds \n");
break;
}
for (i = numwords - 2; i >= posit; i--) {
strcpy(matrix[i + 1], matrix[i]);
}
strcpy(matrix[posit], word2);
posit++;
}
for (i = 0; i < posit; i++) {
printf("%s ", matrix[i]);
}
printf("\n posit is %d\n", posit);
}
I am getting segmentation error?? PLS HElP
#include <stdio.h>
#include <string.h>
void display(char n2[], int x2[], char c1[], int p);
void display(char n2[], int x2[], char c1[], int p)
{
printf("Student Name : %s \n", n2[p]);
printf("Student Roll No : %d \n", x2[p]);
printf("Student Class : %s \n ", c1[p]);
}
int main()
{
char n[50], c[5], n1;
int y, x[8], x1;
int p1 = 0;
printf("Enter the number of students: \n");
scanf("%d", &y);
fflush(stdin);
for (int i = 0; i < y; i++)
{
fflush(stdin);
printf("Enter the Student Name : \n");
scanf("%s", n[i]);
fflush(stdin);
printf("Enter the Student Class : \n");
scanf(" %s", c[i]);
fflush(stdin);
printf("Enter the Student Roll No : \n");
scanf(" %d", &x[i]);
fflush(stdin);
}
fflush(stdin);
printf("Enter the Student Name and Roll Number :\n");
scanf("%s %d", &n1, &x1);
for (int i = 0; i < y; i++)
{
if ((n[i] == n1) && (x[i] == x1))
{
p1 = i;
}
else
{
printf("No Such Entry!!");
}
}
display(n, x, c, p1);
return 0;
}
The error occurs here:
scanf("%s",c[i]);
You are trying to store a string into a char (n[i]).
You should define n and the others as an array of strings instead of a string, for example:
char n[50][ 50 ], c[50][50], n1[50];
Also, you can't compare strings like you do on this line:
if ((n[i]== n1) && (x[i] == x1))
Use strcmp instead to compare strings.
Like i want to put the "return 0" at the right place, everything in the code is working normal.
complie the code and they keep saying about "while before return", i tried to put outside of the code and inside
#include <stdio.h>
#include <stdlib.h>
#define MAXN 100
int menu(){
printf("Menu:\n");
printf("1- Add a value\n");
printf("2- Search a value\n");
printf("3- Print out the array\n");
printf("4- Print out values in a range\n");
printf("5- Print out the array in ascending order\n");
printf("6- Quit?\n");
printf("Enter your operation: ");
int choice;
scanf("%d", &choice);
return choice;
}
int isFul (int*a, int n){
return n==MAXN;
}
int isEmpty (int*a, int n){
return n==0;
}
void add(int value, int*a, int*pn){
a[*pn] = value;
(*pn)++;
}
int search(int x, int *a, int n){
int i;
for (i=0; i<n; i++) if (a[i]==x) return i;
return -1;
}
void printvalueinrange (int*a, int n){
int i, min, max;
printf("\nEnter min value: ");scanf("%d", &min);
printf("\nEnter max value: ");scanf("%d", &max);
for(i=0; i<sizeof(a); i++)
if(a[i]>=min&&a[i]<=max) printf("%d", a[i]);
}
void printarray(int*a, int n){
int i;
for (i=0;i<n;i++) printf("%d", a[i]);
}
void printAsc(int*a, int n){
int** adds =(int**)calloc(n, sizeof(int*));
int i,j;
for(i=0; i<n; i++) adds[i]= &a[i];
int* t;
for (i=0;i<n-1; i++)
for(j=n-1; j>i; j--)
if (*adds[j]< *adds[j-i]){
t=adds[j];
adds[j]=adds[j-1];
adds[j-1]=t;
}
for (i=0;i<n; i++) printf("%d ", *adds[i]);
free(adds);
}
int main(){
int a[MAXN];
int n=0;
int value;
int choice;
do
{ choice= menu();
switch(choice){
case 1:{
if (isFull(a,n)) printf("\n Sorry! The array is full.\n");
else
{ printf ("Input an added value:");
scanf("%d", &value);
add(value, a, &n);
printf("Added!\n");
}
break;
}
case 2:{
if (isEmpty(a,n)) printf("\n Sorry! The array is empty.\n");
else
{ printf ("Input the searched value:");
scanf("%d", &value);
int pos = search(value, a, n);
if (pos<0) printf("Not found!\n");
else printf("Postion is found: %d\n", pos);
} break;
}
case 3:{
if (isEmpty(a,n)) printf("\n Sorry! The array is empty.\n");
else
{
printarray(a,n);
} break;
}
case 4:{
if (isEmpty(a,n)) printf("\n Sorry! The array is empty.\n");
else
{
printvalueinrange(a,n);
} break;
}
case 5:{
if (isEmpty(a,n)) printf("\n Sorry! The array is empty.\n");
else
{
printAsc(a,n);
} break;
}
default: printf ("Goodbye!");
break;
}
while (choice>0 && choice<7);
getchar();
}
return 0;
}
I just adding some word to fit with the requirement :"D
And if u find out any kind of error or mistake that in my code, just points out for me to improve them xd
In this part of main
}
while (choice>0 && choice<7);
getchar();
}
return 0;
}
the while construction occupies a wrong place.
There should be
}
} while (choice>0 && choice<7);
getchar();
return 0;
}
Also in this statement
if (isFull(a,n)) printf("\n Sorry! The array is full.\n");
^^^^^^
there is a typo. There should be
if (isFul(a,n)) printf("\n Sorry! The array is full.\n");
I really need help for this, like i want to print out them and what do i need to adding to this function to work.
#include <stdio.h>
#include <stdlib.h>
#define nameSizeMax 31
#define listSizeMax 100
char studentList[listSizeMax][nameSizeMax];
int listSize;
void addStudent() {
if(listSize == listSizeMax) {
printf("List is full!\n\n");
} else {
printf("Enter name of the student: ");
fflush(stdin);
char name[nameSizeMax];
gets(name);
trim(name);
strupr(name);
strcpy(studentList[listSize], name);
listSize++;
printf("The new student have been added!\n\n");
}
}
int searchStd(char list[][nameSizeMax], char item[], int size) {
int i;
for(i=0;i<size; i++);
if (strcmp(list[i], item) == 0) return 1;
return -1;
}
void removeStudent() {
int i
if (listSize == 0) {
printf("List is empty!\n\n");
} else {
printf ("Enter name of the removed student: ");
fflush(stdin);
char name[nameSizeMax];
gets(name);
trim(name);
strupr(name);
int pos = searchStd(studentList, name, listSize);
}
if(pos == -1) {
printf("This student does not exist!\n\n");
} else {
for (i=pos; i<listSize-1; i++);
strcpy(studentList[i], studentList[i+1]);
printf("This student has been removed from the list: \n\n");
listSize--;
}
}
void fintStudent() {
int i;
printf("Enter the name of student you want to search: ");
fflush(stdin);
char name[nameSizeMax];
gets(name);
trim(name);
strupr(name);
int pos = searchStd(studentList, name, listSize);
if(pos == -1) {
printf("This student does not exist:\n\n");
} else {
printf("\n This is the student list in the order you entered: \n");
for(i=0; i<listSize; i++) printf("%d - %s\n", i, studentList[i]);
printf ("This student's postition in the list is: %d\n\n", pos);
}
}
void printStudentAsc(char list[][nameSizeMax], int size) {
int i,j:
if (listSize == 0) {
printf("List is empty!\n\n");
} else {
char **listP = (char **) calloc(sizeof(char *));
for (i=o; i<size; i++) listP[i] = list[i];
for (i=0; i<size-1; i++)
for(j=i+1; j<size; j++) {
char *ln1,*ln2;
ln1= lastname(listP[i]);
ln2= lastname(listP[j]);
if (strcmp(ln1, ln2) == 1) {
char *tmp = listP[i];
listP[i] =listP[j];
listP[j] = tmp;
}
}
}
for (i = 0; i < (*pn); i++)
{
nameStr(list[i]);
printf("Name[%d] : %s \n", i, list[i]);
}
}
int menu(){
printf("Menu:\n");
printf("1- Add a student\n");
printf("2- Remove a student\n");
printf("3- Search a student\n");
printf("4- Print out the student list in ascending order\n");
int userChoice;
do{
printf("Insert ur operation: "); scanf("%d", &userChoice);
}
}
Edited :'( i really struggle about the printAsc like i dont know what to do next . Its really messed me up here. Only the printStudentAsc.
the Menu interface that im not finish yet but i can hanlde this.
I'm currently having a piece of code here that Im stuck. My problem here is that after I use Add function to add a game into the inventory, I press P to display but it shows that the inventory is empty. So how can I update the new entry into this inventory?
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define SIZE 40
typedef struct {
char gname[30];
char gcomp[30];
double gprice;
} game;
void showMenu(char *choice);
void gameList(game list[], int num);
void HardCodeSix(game list[]);
void Add(game list[], int num);
int main()
{
game list[SIZE];
int count = 0;
char choice;
int num = 0;
HardCodeSix(list);
count = 6;
printf("Welcome to Mini Game-Stop!!\n");
printf("\n");
showMenu(&choice);
choice = toupper(choice);
printf("Your choice is: %c\n", choice);
printf("\n");
while (choice != 'Q')
{
if (choice == 'P')
{
gameList(list, num);
}
else if (choice == 'A')
{
Add(list, num);
}
showMenu(&choice);
choice = toupper(choice);
printf("Your choice is: %c\n", choice);
printf("\n");
}
printf("Thank for using this program. Bye!\n");
return 0;
}
void showMenu(char *choice)
{
printf("** Options: \n");
printf("P....Print list of games.\n");
printf("A....Add more game into the list.\n");
printf("C....Clear all of the choices.\n");
printf("D....Delete one game from the list.\n");
printf("U....Update.\n");
printf("Q....Quit.\n");
printf("\n");
printf("Please enter your choice: ");
scanf(" %c", &*choice);
}
void gameList(game list[], int num)
{
int i;
if (num == 0)
{
printf("Empty List!\n");
printf("\n");
}
for (i = 0; i < num; i++)
{
printf("Title: %s\n", list[i].gname);
printf("Developer: %s\n", list[i].gcomp);
printf("Price: $%.2f\n", list[i].gprice);
}
}
void Add(game list[], int num)
{
printf("Enter game's title: ");
scanf("%s", list[num].gname);
printf("Enter game's developer: ");
scanf("%s", list[num].gcomp);
printf("Price: ");
scanf("%lf", &list[num].gprice);
printf("\n");
}