#include <iostream>
#include <vector>
usingnamespace 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.
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??
#include <iostream>
#include <vector>
usingnamespace 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
"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.
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.