Array is printing out weird symbols

In summary, the program compares two strings representing test scores for a 20 question exam. If the student scores 15 or higher, they pass, otherwise they fail. The program also displays the number of correct and incorrect answers, as well as which question the student got wrong. The issue with the program outputting weird symbols for the incorrect answer has been solved by removing array subscripting and using to_string() or itoa() instead of static_cast.
  • #1
MinusTheBear
22
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. cout << "Congratulations. You passed the exam.\n";
  25. }
  26. else{
  27. cout << "Sorry. You failed the exam.\n";
  28. }
  29. cout << "You got " << correct << " questions correct.\n";
  30. cout << "You missed the following" << wrong << " question(s): ";
  31. for (int element = 0; element < wrong; element++){
  32. cout << 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
  • #2
Just as a side note, you should learn to use the code tags so that your code is readable.
 
  • #3
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.
 
  • #4
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.
 

FAQ: Array is printing out weird symbols

What could be causing my array to print out weird symbols?

There could be several reasons for this issue. One possibility is that the array contains non-printable characters or invalid data. Another possibility is that the array is not properly initialized or has been corrupted. Additionally, the way the array is being accessed or printed could also be causing the issue.

How can I fix my array from printing out weird symbols?

The first step to fixing this issue is to identify the root cause. Check the contents of the array and make sure they are valid and properly formatted. If the array is being accessed or printed, ensure that the correct methods and syntax are being used. If the issue persists, try reinitializing the array or debugging the code for any errors.

Can a corrupted array cause weird symbols to be printed?

Yes, a corrupted array can definitely cause strange symbols to be printed. If the array has been modified or its data has been overwritten, the contents may no longer be recognizable or valid. This can lead to unexpected symbols being printed when the array is accessed or printed.

What should I do if my array is printing out symbols that don't make sense?

If the symbols being printed are not recognizable or make no sense in the context of the array, it is likely that the data in the array has been corrupted or is invalid. Try reinitializing the array or debugging the code to identify and fix any issues. You may also need to check the source of the data being stored in the array to ensure it is properly formatted.

Is there a way to prevent my array from printing out weird symbols in the future?

To prevent this issue from happening again, it is important to properly initialize and manage your arrays. Always make sure the data being stored is valid and properly formatted. It is also helpful to use methods and syntax that are appropriate for the type of data being stored. Debugging and testing your code thoroughly before use can also help prevent unexpected issues with array printing.

Similar threads

Replies
3
Views
974
Replies
2
Views
1K
Replies
15
Views
2K
Replies
7
Views
2K
Replies
3
Views
908
Back
Top