-1

I need to do a program for school that reads few products and their price and then sort them in a list accodring by their price so im using array list to do it but when i print them i get random characters as output

#include <stdlib.h>
#include <stdio.h>

int main(){
    int i = 0;
    char list1[7];
    char list2[7];
    char list3[7];
    while(i <= 3){
        char name;
        int price;
        printf("Give me the product \n");
        scanf("%s", &name);
        printf("Give the price \n");
        scanf("%d", &price);
        if(price == 1){
            list1[i] = list1[i] + name;
        } else if(price == 2){
            list2[i] = list2[i] +name;
        } else if(price == 3){
            list3[i] = list3[i] +name;
        } else {
            printf("Invalid number! \n Give us number 1,2 or 3");
            printf("Give me the product \n");
            scanf("%s", &name);
            ("Give the price \n");
            scanf("%d", &price);
        }
        i = i + 1;
    }
    for (int z = 0; z <= 3; z++){
        printf("%s",list1);
        printf("\n");
        printf("%s",list2);
        printf("\n");
        printf("%s",list3);
        printf("\n");
    }
}
Odar
  • 9
  • 1
  • 4
  • `name` is a character. But you are treating it as char array in `scanf("%s", &name)`. Also `list1[i] = list1[i] + name` what are you trying to do here? – kuro Jun 13 '21 at 14:40
  • 5
    That code is broken from start to end. Do you want to do C or C++? `scanf("%s", &name);` will cause undefined behaviour. `list1[i] = list1[i] + name;` does not do what you probably intend to do (it adds together to characters and stores the result in the first slot of an array). And none of your char arrays is null-terminated, so you get undefined behaviour when printing them. If you want to do C++ I suggest to get a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – Lukas-T Jun 13 '21 at 14:42
  • You are trying to do too much that is new in one program. Try a much simpler problem, like reading *one* product and printing it out. Work the bugs out of that, before you attempt this more complex problem. – Beta Jun 13 '21 at 14:44
  • This question is full of typos. Try some easier program first. – silverfox Jun 13 '21 at 14:44
  • Its C i need to do this program for my final exams with c but i dont know how to do it because i get radnom symbols as a result – Odar Jun 13 '21 at 14:46

3 Answers3

0

Looks like you forgot printf in one of your lines. Change ("Give the price\n"); to printf("give the price\n");.

Sam Autrey
  • 121
  • 1
  • 4
0
#include <stdlib.h>
//Need string.h library to use strcopy
#include <string.h>
#include <stdio.h>

#define MAX_CHAR_SIZE 10

int main(){
    //We define the maximum size of an array to be sure it will not overflow so the maximum character that list1,2,3 can contain is 10 including '/0' since you wanna print it as a string
    char list1[MAX_CHAR_SIZE];
    char list2[MAX_CHAR_SIZE];
    char list3[MAX_CHAR_SIZE];

    char name[MAX_CHAR_SIZE];
    //I prefer unsigned if you know you don't to go in negative value
    unsigned int price;
    //Prefer for over while if you know how many time you need to loop
    for (int i = 0; i < 3; i++){
        printf("Give me the product \n");
        scanf("%s", name);
        printf("Give the price \n");
        scanf("%u", &price);
        if(price == 1){
            //Copy name in list 1, you can't copy an array of x char in a single case of another array, 1 case of an array = 1 char
            strcpy(list1, name);
        } 
        else if(price == 2){
            strcpy(list2, name);
        } 
        else if(price == 3){
            strcpy(list3, name);
        } 
        else {
            printf("Invalid number! \n Give us number 1,2 or 3");
            printf("Give me the product \n");
            scanf("%s", name);
            printf("Give the price \n");
            scanf("%u", &price);
        }
    }
    //No need to loop over this 3 time only 1 is enough
    printf("%s \n",list1);
    printf("%s \n",list2);
    printf("%s \n",list3);
}

I add comment over things I changed to make the initial objectives, IDK if it's the right goal but at least you got the same character in input and output, there was many things that wasn't right in your code you simply can't add the hexadecimal value of two character together to overwrite, you got to make something like list[I] = name[I].

And in C since we don't have the string type you got to create an array of char to make one AND BE SURE TO GET THE RIGHT SIZE TO FIT '\0'.

If it's your final exam a friendly advice, train a lot.

0

It should work if I understand your question

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


#define MAX_LENGTH 10
int main(){
    int n;  //no of products
    printf("Enter no. of products: ");
    scanf("%d", &n);
    char name[n][MAX_LENGTH];  //create multidimensional array to store namme
    unsigned int price[n];     //array to store price. index will be similar to name

    for (int i = 0; i < n; i++){  //Enter details
        printf("Enter name: ");     
        scanf("%s", name[i]);
        printf("Enter price: ");
        scanf("%u", &price[i]);
    }

    int N = n;
    while (N > 0){                          //Run loop until index bigger than zero
        char temp_string[MAX_LENGTH];       //create temporary variable 
        unsigned int temp_price;            //to store values
        int max_price_index = 0;            //Index where max value is stored
        for (int i = 1; i < N; i++){        //start loop from till end to search each  value
            if(price[i] > price[max_price_index])       //If searched value is bigger thar previous one(default at index 0)
                max_price_index = i;                    //the replace it
        }
        strcpy(temp_string, name[N - 1]);               //in next 7 lines name and price at max index is 
        strcpy(name[N - 1], name[max_price_index]);     //swapped with values at index N (which is last of scope)
        strcpy(name[max_price_index], temp_string);

        temp_price = price[N - 1];
        price[N - 1] = price[max_price_index];
        price[max_price_index] = temp_price;

        N--;                //reduce the index by 1 making last value which was preiously maximum out of scope
    }

    for (int i = 0; i < n; i++){        //print details
        printf("Name: %s\nPrice: %u\n", name[i], price[i]);
    }
}