Problem with pointers

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
39
40
41
42
43
44
45
46
#include <iostream>
using namespace std;

template <class T> 
class maximum
{
      public:
             maximum(T num1,T num2)
             {
              Num1 = num1;
              Num2 = num2;          
             }
             T getMax();
      private:
              T Num1;
              T Num2;
};

template<class T>
T maximum<T>::getMax()
{
 return (Num1 > Num2 ? Num1: Num2);                      
}

void getnum(int*,int*);

int main()
{
 int num1, num2;
 cout<<"Enter two integars, seperated by a comma: ";
 getnum(&num1,&num2);
 maximum<int> m(num1,num2);
 cout<<m.getMax();
 system("pause");
 return 0;    
}

void getnum(int* num1,int* num2)
{
     char c;
     cin.getline(c,",");//invalid conversion from 'const char*' to 'int'
     (*num1) = c - '0'; //invalid conversion from 'char*' to 'int'
     cin.getline(c,"\n");//invalid conversion from 'const char*' to 'int'
     (*num2) = c- '0';//invalid conversion from 'char*' to 'int'
}


why am i getting these errors?

Last edited on
First of all, cin.getline() takes a char * for its first argument; the second argument is a streamsize (the size of your first argument; and finally a char for the delimiter, not a character strings as you have it.
the second argument is a streamsize (the size of your first argument; and finally a char for the delimiter, not a character strings as you have it.


I understand this.

First of all, cin.getline() takes a char * for its first argument;

Could you explain this please?
//invalid conversion from 'const char*' to 'int

you are trying to pass it an int and its parameter is const char*

it is the same as if you made a function
1
2
3
4
5
int plustwo (int number)
{
number+=2;
return number;
}


and then you went and tried to pass it something it doesn't want

1
2
string name = "somename";
int nameplustwo = plustwo(name);


or conversely you are asking for an int and it gives const char*

such as
 
string name = plustwo(5);

Last edited on
i wrote this now:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void getnum(int* num1,int* num2)
{
     string s,s_num;
     getline(cin,s,'\n');
     
     size_t comma = s.find(",");
     size_t end = s.find("\0");
     
     s_num=s.substr(0,comma);
     *num1=atoi(s_num.c_str());
   
     s_num=s.substr(comma,end);
     *num2=atoi(s_num.c_str());
     
     
}


it gets value for num1 but why doesn't it find the value for num2?
substr works with range [), so the substring still have the comma character at the beginning an atoi fails
Thanks, I understand it now.
Topic archived. No new replies allowed.