adding numbers from a string array
the data array is a string array that contains student data like name student id but also contains exam marks, i am trying to add all the marks to get a total mark (total should be 12) and im getting this number when i outputted it to test?
19 Replies
what datatype does
data
have?string
as it contains a mix of words
and numbers
Would you please post the full code? $paste
If your code is too long, you can post to https://paste.mod.gg/ and copy the link into chat for others to see your shared code!
np
If each individual element is a number, you will need to parse those numbers individually first, then sum them up
so like 10 lines
of parsing?
then add
Or... a loop of parsing
hows it work that one out
id understand if it was 44
Disregard it
That's about how chars work
You're not working with chars
Oh, I though it was a string by itself, not an array of strings
That said... you also say this array contains data about a student
Why not use a... class?
That way, you could store numbers as numbers
And strings as strings
for (int i = 3; i < 12; i++)
{
int add = int.Parse(data[i]);
sum += add;
}
int dpMark = sum;
this worked
thx
the array is being read from a text file
idk if that is still viable
but i didnt mention it
Does it have to be an unstructured text file, and not something like JSON?
yh my task was to read these txt files with a .mark extension for some reason filled with student info and marks
and use sorting
to output in descending order
and grad ethem
Ah, a school assignment then
Carry on
yh
If you want to be super efficient and assuming all grades are a single number, then append all strings together, parse it as a whole, and then sum it all with bitshifting
Just kidding, don't do that