Char problem

Im working on my vector and trying to create a error catcher of sorts. but my char wont work, i dont get it here it is:

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

using namespace std;

 int main()
 {
     vector<float> student_marks(10);

     for (vector<float>::size_type i = 0; i < 10; i++)
     {
         cout << "Enter marks for student #" << i+1 << ": " << flush;
         cin >> student_marks[i];
     }
 }

void num()
{
    char numbers = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '\0'};
}


I get these errors:

C:\Users\Chay Hawk\Desktop\Vector\main.cpp||In function 'void num()':|
C:\Users\Chay Hawk\Desktop\Vector\main.cpp|19|error: scalar object 'numbers' requires one element in initializer|
C:\Users\Chay Hawk\Desktop\Vector\main.cpp|19|warning: unused variable 'numbers'|
||=== Build finished: 1 errors, 1 warnings ===|

i put char numbers[9] and it said this:

1
2
3
4
5
6
7
8
C:\Users\Chay Hawk\Desktop\Vector\main.cpp||In function 'void num()':|
C:\Users\Chay Hawk\Desktop\Vector\main.cpp|19|error: too many initializers for 'char [9]'|
C:\Users\Chay Hawk\Desktop\Vector\main.cpp|19|warning: unused variable 'numbers'|
||=== Build finished: 1 errors, 1 warnings ===|
 
I changed it again to [1] and it says the same thing as above.

What am i doing wrong i dont get it, i was reading on chars here on the site but i didnt find a solution.
`char' is a single character. If you want an array of char you can do:

 
char numbers[] = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '\0' };

Also, if you're null terminating it like that, you don't need to do the whole {} initialization thing. You can just use quotes:

char numbers[] = "1234567890"; // <- same thing
Last edited on
Hmm, i see. Well i have an error, :

||=== Vector, Debug ===|
C:\Users\Chay Hawk\Desktop\Vector\main.cpp||In function 'int main()':|
C:\Users\Chay Hawk\Desktop\Vector\main.cpp|18|error: 'numbers' was not declared in this scope|
C:\Users\Chay Hawk\Desktop\Vector\main.cpp||In function 'void num()':|
C:\Users\Chay Hawk\Desktop\Vector\main.cpp|25|warning: overflow in implicit constant conversion|
C:\Users\Chay Hawk\Desktop\Vector\main.cpp|25|warning: unused variable 'numbers'|
||=== Build finished: 1 errors, 2 warnings ===|


Code:

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
#include <iostream>
#include <vector>

using namespace std;

void num();

 int main()
 {
     vector<float> student_marks(10);

     for (vector<float>::size_type i = 0; i < 10; i++)
     {
         cout << "Enter marks for student #" << i+1 << ": " << flush;
         cin >> student_marks[i];
     }

     if(student_marks != numbers){
        cout << "Error" << endl;
     }
 }

void num()
{
    char numbers[] = {1234567890};
}


i tried googleing "overflow in implicit constant conversion" but i didnt fina good nderstanding of the problem.
needs to be :

 
char numbers[] = "1234567890";


EDIT: The "overflow" is because it is trying to assign the integer value 1234567890 to a character which can only hold a maximum value of 128.
Last edited on
Ah, crap i didnt see that, sorry. I still get errors:

C:\Users\Chay Hawk\Desktop\Vector\main.cpp||In function 'int main()':|
C:\Users\Chay Hawk\Desktop\Vector\main.cpp|18|error: 'numbers' was not declared in this scope|
C:\Users\Chay Hawk\Desktop\Vector\main.cpp||In function 'void num()':|
C:\Users\Chay Hawk\Desktop\Vector\main.cpp|25|warning: unused variable 'numbers'|
||=== Build finished: 1 errors, 1 warnings ===|

I dont understand this one 'numbers' was not declared in this scope i put void num(); at the top so it should understand what im trying to do? I tried changing it to num() and i tried num.numbers, but nothing. Also you say "a character which can only hold a maximum value of 128." those were only ten characters though? or do the numbers have a high bit value??

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
#include <iostream>
#include <vector>

using namespace std;

void num();

 int main()
 {
     vector<float> student_marks(10);

     for (vector<float>::size_type i = 0; i < 10; i++)
     {
         cout << "Enter marks for student #" << i+1 << ": " << flush;
         cin >> student_marks[i];
     }

     if(student_marks != numbers){
        cout << "Error" << endl;
     }
 }

void num()
{
    char numbers[] = "1234567890";
}
Last edited on
Numbers is out of scope in main() because it is both created and destroyed in num(). Instead of having a function you can just put:

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

using namespace std;

char numbers[] = "1234567890";

 int main()
 {
     vector<float> student_marks(10);

     for (vector<float>::size_type i = 0; i < 10; i++)
     {
         cout << "Enter marks for student #" << i+1 << ": " << flush;
         cin >> student_marks[i];
     }

     if(student_marks != numbers){
        cout << "Error" << endl;
     }
 }


EDIT: By sticking the numbers between braces it told the compiler you wanted to assign that value but a char is only 8-bit so the highest a (signed) char can be is "01111111" which is 127. An unsigned char would take advantage of the last bit so the max would be 255.

char numbers[] = { 1234567890 }; // tries to stick the number 1,234,567,890 into the 8-bit value (which it can't hold thus the overflow)
char numbers[] = "1234567890"; // sticks the ascii value for 1, 2, 3, etc into the array of 8-bit values
Last edited on
if(student_marks != numbers){
cout << "Error" << endl;
}


This code is invalid because you cannot compare vector of floats with character array.
"By sticking the numbers between braces it told the compiler you wanted to assign that value but a char is only 8-bit so the highest a (signed) char can be is "01111111" which is 127. An unsigned char would take advantage of the last bit so the max would be 255.

char numbers[] = { 1234567890 }; // tries to stick the number 1,234,567,890 into the 8-bit value (which it can't hold thus the overflow)
char numbers[] = "1234567890"; // sticks the ascii value for 1, 2, 3, etc into the array of 8-bit values "

Ah i see now.

"This code is invalid because you cannot compare vector of floats with character array."

So how can i check if the user inputs something other than a number and then give an error?
First of all take into acccount that you are entering float numbers, because your vector has elements of type float. So a do not see any sense to compare float numbers with integer digits. The overloaded operator >> for cin does all needed checks. It does not allow to enter something else than a float number.

Maybe you should to declare a vector of integers?
But when i enter a letter it screws up and exits, i want it to catch the error and not do that, i think i understand what you mean, i'll try it and be back!
bump ok i tried a few things and they didnt work. I tried making a string that catches the user input that isnt a number but it didnt work. Maybe i did it wrong but idk.
Last edited on
Topic archived. No new replies allowed.