employee exercise

Hello , I'm just starting to learn coding in c++ , I am trying to write a program that lets user enter some info and to be printed .
Here is the code

#include <iostream>

using namespace std;

int main()
{
int e ;
cout << " how many employees ? " ;
cin >> e ;
for ( int c = 0 ; c < e ; c ++)
{
char n [e] ;
int w [e] ;
cout << " enter the name of the employee ? " ;
cin >> n ;
cout << " enter the wage of the employee ? " ;
cin >> w ;
}
return 0;
}

i get the following error's

D:\c++\semos_c++\file\cas7_zadaca2_add2\main.cpp|17|error: no match for 'operator>>' (operand types are 'std::istream {aka std::basic_istream<char>}' and 'int [e]')|




Lines 12-13: This is not standards compliant C++, although some compiler allow variable length arrays as a non-standard extension. Standard C++ requires that an array dimension be known at compile time.

Line 12-13: You need a doubly subscripted array here or an array of std::string (preferred). As you have it, your names are a single character in length (assuming you meant e to dimension the number of employees.
Consider:
 
  char[30][4];  // Allow 4 names of 30 characters each 


Lines 12-13: Caution: n and w go out of scope here at line 18.

Line 15,17: You want to read into the c'th element.

Line 17: w is an array of ints. You can't cin to an array of ints.

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
using namespace std;

const int MAX_EMP = 10;

int main()
{   int e;
    string n[MAX_EMP];
    int w[MAX_EMP];
         
    cout << " how many employees ? " ;
    cin >> e ;
    for ( int c = 0 ; c < e ; c ++)
    {   cout << " enter the name of the employee ? " ;
        cin >> n[c];
        cout << " enter the wage of the employee ? " ;
        cin >> w[c];
    }
    return 0;
}

Last edited on
First, please use code tags when posting code. See http://www.cplusplus.com/articles/jEywvCM9/

You do declare the w as an array of integers. There is no formatted input operator that would populate a whole array.

The wording of your couts hints that you intend to read just one integer. If the w has e integers, which of them you want to store the value in?


That is not the only problem.

You declare two arrays that have e elements each. The arrays are statically allocated. The compiler has to know during compilation what the e is. It cannot, for the user is not there (and every run of the program can have different e). C++ requires dynamic memory allocation for this situation. The C++ standard library has std::vector container that is a dynamically allocated array.
See http://www.cplusplus.com/reference/vector/vector/operator[]/ for example.

A name is an array of characters, but how long can one name be? Again, dynamic allocation is required. Again, the library has an easy solution, std::string. See http://www.cplusplus.com/reference/string/string/operator%3E%3E/


You seem to want to store the wage for each of e employees. You create an array for e wages. You create it inside a loop. The array exists only to the end of the body of the loop. To the end of the iteration. You repeat this creation and destruction e times. No wages are stored anywhere after the loop.

Solution. Create the "array" before the loop. Outside the loop. Then it exists after the loop.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <string>
#include <vector>
#include <iostream>

int main()
{
  int e = 0;
  std::cin >> e;
  std::vector<std::string> names( e );
  for ( int c = 0 ; c < e ; ++c ) {
    std::cin >> names[c];
  }
  std::cout << "=======\n";
  for ( auto name : names ) {
    std::cout << name << '\n';
  }
  return 0;
}
Topic archived. No new replies allowed.