Array is printing out weird symbols

Click For Summary

Discussion Overview

The discussion revolves around a programming issue related to comparing two strings that represent test scores for a 20-question exam. Participants explore the problem of incorrect output when displaying the numbers of questions missed, specifically the appearance of weird symbols instead of expected numerical values.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes the problem with their code, noting that the output for missed answers contains weird symbols and highlighting specific lines of code that may be causing the issue.
  • Another participant suggests that array subscripting on the string is problematic since the length of elements is uncertain, recommending concatenation instead and questioning the use of static_cast.
  • A later reply indicates that using to_string() resolved the issue, although there remains a concern about printing double digits correctly.
  • Some participants emphasize the importance of using code tags for better readability of code snippets.

Areas of Agreement / Disagreement

Participants generally agree on the problematic nature of the original code and the need for adjustments, but there is no consensus on the best approach to handle the output of double-digit numbers.

Contextual Notes

There are unresolved issues regarding the handling of multi-digit question numbers and the specific implementation details of the input validation within the larger program context.

MinusTheBear
Messages
22
Reaction score
0

Homework Statement


Compare two strings which represent test scores for a 20 question exam. If the student scores 15 or higher, they pass. Less than that they fail. Indicate whether a student passes or fails. Display how many answers they got right and wrong. Display which number they got wrong on the exam.

I simplified the problem at hand (it's actually a lot longer) and removed a lot of my code except the portion that is relevant. I made sure this portion of the code runs. The problem is the output for the answers they got wrong are weird symbols. I made bold the lines that are probably causing an issue. The code runs as expected with every test trial the professor gave EXCEPT for printing out the numbers they missed.

Homework Equations


n/a

The Attempt at a Solution


  1. #include <iostream>
  2. #include <string>
  3. #include <cctype>
  4. using namespace std;

  5. const int NUM_QUESTIONS = 20;
  6. int main()
  7. {
  8. int pass = 15;
  9. int correct = 0;
  10. int wrong = 0;
  11. string missed;
  12. string key = "BDAACABACDBCDADCCBDA";
  13. string student = "BDAACABACDBCDADCCBDD"; // Only 1 answer is wrong, answer 20
  14. for (int answer = 0; answer < NUM_QUESTIONS; answer++){
  15. if (key[answer] == student[answer]){
  16. correct++;
  17. }
  18. else{
  19. missed[wrong] = static_cast<char>((answer+1));
  20. wrong++;
  21. }
  22. }
  23. if (correct >= pass){
  24. count << "Congratulations. You passed the exam.\n";
  25. }
  26. else{
  27. count << "Sorry. You failed the exam.\n";
  28. }
  29. count << "You got " << correct << " questions correct.\n";
  30. count << "You missed the following" << wrong << " question(s): ";
  31. for (int element = 0; element < wrong; element++){
  32. count << missed[element] << " ";
  33. }
  34. return 0;
  35. }
When it runs I get this:
Congratulations. You passed the exam.
You got 19 questions correct.
You missed the following 1 question(s):
 
Physics news on Phys.org
Just as a side note, you should learn to use the code tags so that your code is readable.
 
Line 20 is problematic. You shouldn't use array subscripting on the string, because you don't know how long the elements are. (Some questions have multiple digits). Just concatenate the result onto the string. And you shouldn't use static_cast here. I think you can use to_string() or itoa() to convert to a string.
 
phinds said:
Just as a side note, you should learn to use the code tags so that your code is readable.

How do I do this?

Khashishi said:
Line 20 is problematic. You shouldn't use array subscripting on the string, because you don't know how long the elements are. (Some questions have multiple digits). Just concatenate the result onto the string. And you shouldn't use static_cast here. I think you can use to_string() or itoa() to convert to a string.

This solved it! Thank you! I was using to_string() originally, but the subscripting must've messed it up. I'm not sure why, though. In the main part of the real program I have input validation within a class so I know for a fact each element is 1 character and each string is 20 characters. I was also using static_cast on another example which worked fine -- and it was similar to this. Whatever it was it fixed it. Thanks!

The only issue now is getting it to print out double digits as 10, 11, etc. and not 1 0 1 1, but this is a start. Thanks.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
1K
Replies
8
Views
2K
  • · Replies 18 ·
Replies
18
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
9
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 15 ·
Replies
15
Views
4K