i was new with array for C programming. Anybody can help me? I want to print all array that saved from the assignment. Please help!
#include <stdio.h>
int main(){
const char *a[3];
char s[100];
int data, i=0;
back:
printf("Please insert name: ");
scanf(" %s", &s);
a[i] = s;
printf("Do you want to add data?: ");
scanf("%d", &data);
if (data==1){
i++;
goto back;}
else{
printf("%s", a[0] , a[1] , a[2]);}
return 0;
}
My suggestion would be never to use goto statement in C , it really mess up the readability of your source code. Also, go through this link, Why goto statement is not generally used.
Apart from this, you code can be simple re-written like this using for-loop statement.
#include <stdio.h>
int main(){
char *a[3];
char s[100];
int data = 1, i;
for(i=0; i<3 && data == 1; i++) {
printf("Please insert name: ");
scanf(" %s", &s);
a[i] = s;
printf("Do you want to add data?: ");
scanf("%d", &data);
}
for(i=0; i<3; i++)
printf("%s", a[i]);
return 0;
}
#include <stdio.h>
int main()
{
const char *a[3];
char s[100];
int data, i = 0, j;
back: printf("Please insert name: ");
scanf(" %s", &s);
a[i] = s;
printf("Do you want to add data?: ");
scanf("%d", &data);
i++;
if (data == 1)
{
goto back;
}
else
{
for (j = 0; j < i; j = j + 1)
printf("%s ", a[j]);
}
return 0;
}
Related
I am trying to change the sorting of a the arr list which could consist of zero, one, two as the inputted and stored values for arr. The stringreplace function is meant to shift every single element by one so the new sorting would be one, two, zero. I am trying to replace the elements with one another by using the strncpy function but I think it is a bit faulty, how could i fix this?
strncpy function
char stringreplace( char a[], int b){
for(int j = 0; j > b -1; j++){
strncpy(a[j], a[j+1], sizeof(a));}
for(int j = 0; j > b; j++){
printf("%s",a[j]);}
}
main function
int main()
{
char input[100];
char arr[100]= {0};
int number;
printf("Input the number of strings: ");
scanf("%d", &number);
for(int i= 0; i < number; i++){
printf("Input the number of strings: ");
scanf("%s", input);
arr[i] = input;
}
stringreplace(arr, number);
return 0;
}
You may consider allocating strings dynamically, assigning a pointer for each string into an array words, and then rotating each pointer in the array to the left.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void lrot_words(char *words[], int n);
int main(void)
{
char *p, word[100], *words[100];
int i, num_words;
printf("Enter the number of words: ");
scanf("%d", &num_words);
for(i = 0; i < num_words; i++){
printf("Enter a word: ");
scanf("%s", word);
if ((p = malloc(strlen(word) + 1)) == NULL) {
fprintf(stderr, "Error: malloc failed\n");
exit(EXIT_FAILURE);
}
words[i] = strcpy(p, word);
}
lrot_words(words, num_words);
for (i = 0; i < num_words; i++) {
printf("%s\n", words[i]);
}
return 0;
}
void lrot_words(char *words[], int n)
{
char *temp = words[0];
int i;
for (i = 0; i < n - 1; i++) {
words[i] = words[i+1];
}
words[i] = temp;
}
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;
}
I just got an exercise of programming an array of struct of 2(or more) books with specified variables. Insert variables from keyboard and then find out if Publisher is "Kim Dong" then change the price of Book to 0.
Here is my code [I think something is wrong in collating step but cannot find out why :((]:
typedef struct book {
char *author[2000];
char *id[2000];
char *name[2000];
int *price;
char *publisher[2000];
} sach;
main()
{
int i;
sach Books[10];
for(i = 0; i < 2; i++)
{
printf("Nhap thong tin cua sach thu %d:\n", i+1);
fflush(stdin);
scanf("%s", &Books[i].name);
fflush(stdin);
scanf("%s", &Books[i].author);
fflush(stdin);
scanf("%s", &Books[i].id);
fflush(stdin);
scanf("%d", &Books[i].price);
fflush(stdin);
scanf("%s", &Books[i].publisher);
}
char NXB[8] ="KimDong";
for(i = 0; i < 2; i++)
{
int j = 0;
while( ((int)NXB[j]) == ((int)Books[i].publisher[j]) )
{
j++;
}
if(j == 6)
{
Books[i].price = 0;
}
}
for(i = 0; i < 2; i++)
{
printf("\nTen sach: %s", Books[i].name);
printf("\nID sach la: %s", Books[i].id);
printf("\nTac gia la: %s", Books[i].author);
printf("\nNXB la: %s", Books[i].publisher);
printf("\nGia sach la: %d", Books[i].price);
}
}
What's wrong?
Not sure what you actually want, but it's probably something like this:
#include <stdio.h>
typedef struct book {
char author[2000];
char id[2000];
char name[2000];
int price;
char publisher[2000];
}sach;
int main() {
int i;
sach Books[10];
for (i = 0; i<2; i++) {
printf("Nhap thong tin cua sach thu %d:\n", i + 1);
scanf("%s", &Books[i].name);
scanf("%s", &Books[i].author);
scanf("%s", &Books[i].id);
scanf("%d", &Books[i].price);
scanf("%s", &Books[i].publisher);
}
char NXB[8] = "KimDong";
for (i = 0; i<2; i++) {
int j = 0;
while ((NXB[j]) == (Books[i].publisher[j])) {
j++;
}
if (j == 6) {
Books[i].price = 0;
}
}
for (i = 0; i<2; i++) {
printf("\nTen sach: %s", Books[i].name);
printf("\nID sach la: %s", Books[i].id);
printf("\nTac gia la: %s", Books[i].author);
printf("\nNXB la: %s", Books[i].publisher);
printf("\nGia sach la: %d", Books[i].price);
}
}
fflush(stdin) is useless BTW.
The other answer concerning strcmp probably applies too here.
There is still room for improvement.
For string comparison use strcmp(str1,str2).
Note that if str1 is equal to str2, the return value will be 0. Contrary to the usual behavior of similar functions.
The goal of my code is to have the user, put in any amount of students as an integer and then have the program ask over and over to set a name too every integer (student)
I've been trying so many different things and I've been working on this without using any outside help for hours but I just couldn't figure this out. (if its something obvious, please don't get supermad, I'm only a beginner)
#include <cs50.h>
#include <string.h>
#include <stdio.h>
int main (void)
{
printf("How many students are there? ");
int amount = atoi(GetString());
printf("amount = %i\n", amount);
char *names[amount];
for(int i = 0; i < amount; i++)
{
printf("Enter the ellement #%d :", i +1);
scanf("%s", names[i]);
}
for (int i = 0; i == 0;)
{
printf("Acces student: ");
string search = GetString();
int searchnr = atoi(search);
printf("Student #%d is %s\n", searchnr, names[searchnr]);
}
}
>
}
The obvious solution:
for (int i = 0; i < amount; i++) {
printf("Enter element #%d: ", i + 1);
names[i] = GetString();
}
As to the second loop: it's an infinite loop. What is the terminating condition? You need to put that into the condition of the for loop else it will never terminate.
If your intent is getting an infinite loop, then a more readable, less confusing, more idiomatic solution is
while (1) {
// ...
}
or
for (;;) {
// ...
}
You need to reserve space for those strings:
char *names[amount];
char s[100];
for(int i = 0; i < amount; i++)
{
printf("Enter the ellement #%d :", i +1);
scanf("%s", s);
names[i] = strdup(s);
}
or
char *names[amount];
char s[100];
size_t len;
for(int i = 0; i < amount; i++)
{
printf("Enter the ellement #%d :", i +1);
scanf("%s", s);
len = strlen(s);
names[i] = malloc(len + 1);
strcpy(names[i], s);
}
And this for loop:
for (int i = 0; i == 0;)
does nothing, what do you want to do? (if you want to loop forever you can use for(;;))
Using malloc
char temp[50];
for(int i = 0; i < amount; i++)
{
printf("Enter the ellement #%d :", i +1);
scanf("%s", temp);
names[i]=malloc(strlen(temp)+1);
strcpy(names[i],temp);
}
Using strdup
char temp[50];
for(int i = 0; i < amount; i++)
{
printf("Enter the ellement #%d :", i +1);
scanf("%s", temp);
names[i] = strdup(temp);
}
All your answers were great guys, but this was the final solution:
#include <cs50.h>
#include <string.h>
#include <stdio.h>
int main (void)
{
printf("How many students are there? ");
int amount = atoi(GetString());
char *names[amount];
for(int i = 0; i < amount; i++)
{
printf("Enter the ellement #%d :", i +1);
names[i + 1] = GetString();
}
for (int i = 0; i == 0;)
{
printf("Acces student: ");
int search = atoi(GetString());
printf("Student #%d is %s\n", search, names[search]);
}
}
and I know I have an infinite loop there, that was just temporary so I don't have to rerun the command all the time.
This is a homework problem. I have a C program that takes user input for a number of people's first names, last names, and ages. Right now it works and prints out the names to the console correctly, but it is not printing out the right ages, and I can't figure out what I'm doing wrong. Here's my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int choice;
int i = 0;
int x,k,l;
fputs("How many people would you like to add? ", stdout);
scanf(" %d", &choice);
fflush(stdout);
int ch;
while((ch = getchar()) != EOF && ch != '\n');
if (ch == EOF)
{
}
char firstName[choice][20];
char lastName[choice][20];
int age[choice][3];
char first[20];
char last[20];
int a[3];
for (x = 0; x < choice; x++)
{
for (l = 0; l < 3; l++)
{
age[x][l] = 0;
a[l] = 0;
}
}
while(i < choice)
{
printf("Enter the first name of person ");
printf(" %d", i);
printf(": ");
fgets(first, 20, stdin);
for (k = 0; k < 20; k++)
{
firstName[i][k] = first[k];
}
i++;
}
i = 0;
while(i < choice)
{
printf("Enter the last name of person ");
printf(" %d", i);
printf(": ");
fgets(last, 20, stdin);
for (k = 0; k < 20; k++)
{
lastName[i][k] = last[k];
}
i++;
}
i = 0;
while(i < choice)
{
fputs("Enter the age of person ", stdout);
printf(" %d", i);
printf(": ");
scanf(" %d", &a);
fflush(stdout);
for (l = 0; l < 3; l++)
{
age[i][l] = a[l];
}
i++;
}
int sh;
while((sh = getchar()) != EOF && sh != '\n');
if (sh == EOF)
{
}
for (x = 0; x < choice; x++)
{
printf("First name ");
printf(": ");
printf("%s ", firstName[x]);
printf("\n");
printf("Last name ");
printf(": ");
printf("%s ", lastName[x]);
printf("\n");
printf("Age ");
printf(": ");
printf("%d ", &age[x]);
printf("\n");
}
return 0;
}
If you copy/paste this code it will run, but the age outputted will be incorrect. Can anyone tell me why this is? Thank you!
scanf(" %d", &a);
That should be:
scanf(" %d", &a[0]);
And the printf should be printf("%d", age[x][0]);
You want to read into the first element of the array, not the entire array. You want to print out the first element of the array, not the address of the array.
A better solution would probably be not to make age an array of 3 at all. Each person only has one age. The changes would be:
int age[choice];
int a;
scanf(" %d", &a);
age[choice] = a;
printf("%d ", age[x]);