Passing Char Array Problem?

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
#include<iostream>
#include<cstring>
#include<stdio.h>
#include "gpa.h"

using namespace std;

void gpa::getInput()
{

cout << "Enter in your grade: ";
gets(input);
count += 1;
processInput(&input);
}

void gpa::processInput(char* entry)
{
char threep[] = "b+";
char three[] = "b";
char threem[] = "b-";
char twop[] = "c+";
char two[] = "c";
char twom[] = "c-";

if(strcmp(entry,threep) == 0)
{
cout << "You entered in b+" << endl;
}

}


So the user enters a character like c+ or b- and then I pass that input into another function which figures what he entered. I'm using strcpy and passing by reference but I keep on getting these weird errors.

1
2
3
4
5
6
7
8
9
10
11
12
[ansm@pc7.cs.ucdavis.edu GPACalculator]$ g++ -Wall gpa.cpp gpa.h main.cpp
gpa.cpp: In member function 'void gpa::getInput()':
gpa.cpp:14:20: error: no matching function for call to 'gpa::processInput(char (*)[3])'
gpa.cpp:14:20: note: candidate is:
gpa.h:12:7: note: void gpa::processInput(char*)
gpa.h:12:7: note:   no known conversion for argument 1 from 'char (*)[3]' to 'char*'
gpa.cpp: In member function 'void gpa::processInput(char*)':
gpa.cpp:20:6: warning: unused variable 'three' [-Wunused-variable]
gpa.cpp:21:6: warning: unused variable 'threem' [-Wunused-variable]
gpa.cpp:22:6: warning: unused variable 'twop' [-Wunused-variable]
gpa.cpp:23:6: warning: unused variable 'two' [-Wunused-variable]
gpa.cpp:24:6: warning: unused variable 'twom' [-Wunused-variable]


Help please? thanks guys! I really enjoy the community around here they've helped me so much already. Trying to learn c++ this summer before I actually need it in College haha :)

Also here is my header file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

#include<iostream>
//#include<string>
using namespace std;

class gpa
{

public:


        void getInput();//gets input from user
        void processInput(char*);
        void computeGPA();
        void printGPA();
private:
        char input[3];
        int count;
        float gpa;

};
~

and my main.cpp file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[ansm@pc7.cs.ucdavis.edu GPACalculator]$ vi main.cpp
#include<iostream>
#include<string>
#include "gpa.h"

using namespace std;

int main()
{

gpa GPA;
GPA.getInput();


}

Last edited on
When passing an array into a function it gets passed by the address of the array automatically, meaning you don't need the & before it. In this case change processInput(&input); to processInput(input);

Also on a side note I wouldn't call a class and a variable the same thing (you have a gpa class and a gpa float) as, at least in my compilers case (Visual Studio), it gets confused.

Everything else you have are just warnings as you haven't used those variables yet.
1
2
3
4
5
6
7
8
9
void gpa::getInput()
{

cout << "Enter in your grade: ";
gets(input);
count += 1;
processInput(&input);
}

modify this line:
 
processInput(input);

the array name itself points to the starting address of array, it doesnt make sense to use &input.
Topic archived. No new replies allowed.