Alternative to .length()?

I keep getting an error that says .length() is not usable with a char, and I can't seem to figure out an alternative. Any help would be much appreciated.

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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82

#include <iostream>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <string>
#include <cstring>

using namespace std;

void get_data(ifstream &inp, char first[], char last[], char answer[]);
void single_grade(char key[], char first[], char last[], char answer[], double grade[], int x);
void output(char key[], char first[], char last[], char answer[], double grade[]);

int main(){
    string input_filename;
    char first[9], last[9], answer[15], key[15];
    double grade[10];

    cout << "Enter the name of the file where the ungraded tests are located: ";
    cin >> input_filename;
    ifstream inp;
    inp.open(input_filename.c_str());
    
    inp >> key;
    
    for(int i = 0; i < 9; i++){
        get_data(inp, first, last, answer);
        single_grade(key, first, last, answer, grade, i);
        output(key, first, last, answer, grade);
    }
    inp.close();
    return EXIT_SUCCESS;
}
void get_data(ifstream &inp, char first[], char last[], char answer[]){
    int i = 0;
    inp >> first >> last;
    while(inp.peek() != '\n'){
    inp >> answer[i];
    i++;
    }
}

void single_grade(char key[], char first[], char last[], char answer[], double grade[], int x){
    double score = 0;
    double ppts = 0;
    
        for(int i = 0; i < 14; i++){
            if(toupper(answer[i]) == (toupper(key[i]))){
                score++;
                
            }
               ppts++;
            
        }
        grade[x] = (score/ppts)*100;
        cout << grade << endl;
        
       cout << endl;
       cout << endl;

}
void output(char key[], char first[], char last[], char answer[], double grade[]){
    for(int i = 0; i < first.length();i++){
        cout << first[i];
    }
    cout << " ";
    for(int i = 0; i < last.length(); i++){
        cout << last[i] << endl;
    }
        cout << "______________________________________" << endl;
    for(int i = 0; i < 8; i++){
        cout << i+1 << "."<<  answer << "(" << key << ")" <<  " ";
    }
    cout << endl;
    for(int k = 8; k < 15; k++){
        cout << k+1 << "."<<  answer << "(" << key << ")" <<  " ";
    }
        cout << endl;
        cout << endl;
}
arrays don't store length information you have to pass that separately. On a null terminated char array you can use strlen.
http://www.cplusplus.com/reference/cstring/strlen/?kw=strlen
The issue that you are having is that array doesn't have a length method.

The solution is that you want to use the size method.

Here is the reference page:
http://www.cplusplus.com/reference/array/array/?kw=array

Remember that if you are looking for a method that a certain class has you want to go the to the reference page on this website and search for the class. Also you have a search function at the top of the page you can use, too.

I hope this helps.

- Hirokachi


I completely realize what he was talking about now sorry about the random things that i said in this post.
Last edited on
I am not familiar with strlen. How would I replace first.length() with it? Am I going to need a new function?
Instead of calling x.length(), use strlen(x).
Wow, thank you. I didn't think it was that simple. Now for whatever reason, my loop isn't terminating and my outputs are slightly messed up. I will continue to work on it though. If anyone can see anything wrong, i'd appreciate any help. I'll post my input and outputs.

Input file:
ACDBCBDCCACDACC
George Washington abcdabcdabcdccc
John Adams abaa aaccacdacc
Thomas Jefferson ABcddaACbbcddda
John Kennedey cbbADcbbacdbadc
Jimmy Carter abdcad abdcbaCD
George Bush CCddaabbccaaddc
Barack Obama acdBCbdccacd cc
Bill Clinton AcdbCBdccacaacc
Richard Nixon Acdbcacccacdacc

Outputs:


n Washington
______________________________________
1.a(A) 2.b(C) 3.c(D) 4.d(B) 5.a(C)
6.b(B) 7.c(D) 8.d(C) 9.a(C) 10.b(A)
11.c(C) 12.d(D) 13.c(A) 14.c(C) 15.c(C)

John Adams
______________________________________
1.a(A) 2.b(C) 3.a(D) 4.a(B) 5.a(C)
6.a(B) 7.c(D) 8.c(C) 9.a(C) 10.c(A)
11.d(C) 12.a(D) 13.c(A) 14.c(C) 15.c(C)

Jefferson
______________________________________
1.A(A) 2.B(C) 3.c(D) 4.d(B) 5.d(C)
6.a(B) 7.A(D) 8.C(C) 9.b(C) 10.b(A)
11.c(C) 12.d(D) 13.d(A) 14.d(C) 15.a(C)

John Kennedey
______________________________________
1.c(A) 2.b(C) 3.b(D) 4.A(B) 5.D(C)
6.c(B) 7.b(D) 8.b(C) 9.a(C) 10.c(A)
11.d(C) 12.b(D) 13.a(A) 14.d(C) 15.c(C)

Jimmy Carter
______________________________________
1.a(A) 2.b(C) 3.d(D) 4.c(B) 5.a(C)
6.d(B) 7.a(D) 8.b(C) 9.d(C) 10.c(A)
11.b(C) 12.a(D) 13.C(A) 14.D(C) 15.c(C)

George Bush
______________________________________
1.C(A) 2.C(C) 3.d(D) 4.d(B) 5.a(C)
6.a(B) 7.b(D) 8.b(C) 9.c(C) 10.c(A)
11.a(C) 12.a(D) 13.d(A) 14.d(C) 15.c(C)

Barack Obama
______________________________________
1.a(A) 2.c(C) 3.d(D) 4.B(B) 5.C(C)
6.b(B) 7.d(D) 8.c(C) 9.c(C) 10.a(A)
11.c(C) 12.d(D) 13.c(A) 14.c(C) 15.c(C)

Bill Clinton
______________________________________
1.A(A) 2.c(C) 3.d(D) 4.b(B) 5.C(C)
6.B(B) 7.d(D) 8.c(C) 9.c(C) 10.a(A)
11.c(C) 12.a(D) 13.a(A) 14.c(C) 15.c(C)
> char first[9], last[9], answer[15], key[15];
out of bounds access. You didn't reserve enough space for your input (also, keep in mind that you need the null terminator character)
What's the null terminator character? Sorry about my noobness.
I figured the program would terminate when i = 8?
because its for(int i = 0; i < 9; i++)
closed account (48T7M4Gy)
char first[9], last[9], answer[15], key[15];

As people are trying to advise you the size of each of these arrays is known and set by you. There is no magic.

'first' has 9 elements whether you have the last one as a terminal character or not. You can store up to and including 9 char's in first. Nothing more complicated than that.

If you want to read the 9 characters, your for loop will do it.

line 37: inp >> first >> last; is a problem because you are not covering the case where the user inputs more than 9 characters.
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

int main(){
    std::cout << "Input: ";
    char first[9];
    
    std::cin >> first;
    std::cout << "Output: " << first;   
    
    return 0;
}

Input: 123456789
Output: 123456789 
Exit code: 0 (normal program termination)

Input: 123
Output: 123 
Exit code: 0 (normal program termination)


It is not unusual to read the input into a buffer array of say 100 or 200 characters and only using it after appropriate error checking is done on size/length etc.
Last edited on
Okay, thank you. I fixed it now to where the name outputs I am getting are normal, I am just missing the last one and it's not ignoring spaces for some reason.
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

int main(){
    std::cout << "Input: ";
    char first[9], second[9];
    
    std::cin >> first >> second;
    std::cout << "Output: " << first << ' ' << second;  
    
    return 0;
}

Input: as for
Output: as for 
Exit code: 0 (normal program termination)


Try this and you will see hopefully that the space splits the input.
getline() if you want spaces read the way you want.
Topic archived. No new replies allowed.