C
C#10mo ago
zzz

adding numbers from a string array

int dpMark = int.Parse((data[3] + data[4] + data[5] + data[6] + data[7] + data[8] + data[9] + data[10]));
int dpMark = int.Parse((data[3] + data[4] + data[5] + data[6] + data[7] + data[8] + data[9] + data[10]));
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?
No description
19 Replies
Buddy
Buddy10mo ago
what datatype does data have?
zzz
zzzOP10mo ago
string as it contains a mix of words and numbers
Buddy
Buddy10mo ago
Would you please post the full code? $paste
MODiX
MODiX10mo ago
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!
zzz
zzzOP10mo ago
np
Angius
Angius10mo ago
If each individual element is a number, you will need to parse those numbers individually first, then sum them up
zzz
zzzOP10mo ago
so like 10 lines of parsing? then add
Angius
Angius10mo ago
Or... a loop of parsing
zzz
zzzOP10mo ago
hows it work that one out id understand if it was 44
Angius
Angius10mo ago
Disregard it That's about how chars work You're not working with chars
Buddy
Buddy10mo ago
Oh, I though it was a string by itself, not an array of strings
Angius
Angius10mo ago
No description
Angius
Angius10mo ago
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
zzz
zzzOP10mo ago
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
Angius
Angius10mo ago
Does it have to be an unstructured text file, and not something like JSON?
zzz
zzzOP10mo ago
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
Angius
Angius10mo ago
Ah, a school assignment then Carry on
zzz
zzzOP10mo ago
yh
FusedQyou
FusedQyou9mo ago
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
Want results from more Discord servers?
Add your server