I have a problem with strings

Expected primary expression before char
I also want to sort the characters in alphabetical order, but i don't know how.

#include <stdio.h>
#include <string.h>
void introduction(){
printf("\nWelcome!\n");
}
void caps(char name1,char name2,char name3,char name4,char name5){

}
main(){
char name1[20], name2[20], name3[20], name4[20], name5[20];
int qty[5];
introduction();
printf("Input 5 stock names:\n");
gets(name1);
gets(name2);
gets(name3);
gets(name4);
gets(name5);
caps(char name1,char name2,char name3,char name4,char name5);
printf("Input quantity of each item:\n%s ------- ", name1);
scanf("%d", &qty[0]);
printf("%s ------- ", name2);
scanf("%d", &qty[1]);
printf("%s ------- ", name3);
scanf("%d", &qty[2]);
printf("%s ------- ", name4);
scanf("%d", &qty[3]);
printf("%s ------- ", name5);
scanf("%d", &qty[4]);

}
This is C code. Are you deliberately coding in C, or would you switch to C++? This is much, much easier with proper C++ strings and io.
the application i'm using currently is c++. #include <stdio.h>
#include <string.h> are the only Preprocessor directives i know.
#include <stdio.h> is C. The C++ equivalent is #include <cstdio>

But if you're happy to use C++, use C++ strings, and use vectors instead of arrays.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>

using std::vector;
using std::string;

int main()
{
  vector<string> names;
  vector<int> quantity;
  std::cout << "Enter five names" << std::endl;

  for (int i = 0; i<5; i++)
  {
    string temp;
    std::cin >> temp;
    names.push_back(temp);
  }

  std::cout << "Enter five quantities" << std::endl;

  for (int i = 0; i<5; i++)
  {
    int temp;
    std::cin >> temp;
    quantity.push_back(temp);
  }

  std::sort(names.begin(), names.end());

    for (int i = 0; i<5; i++)
  {
    std::cout << names[i] << " ";
  }

}
Topic archived. No new replies allowed.