1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Array is printing out weird symbols

  1. Feb 28, 2017 #1
    1. The problem statement, all variables and given/known data
    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.

    2. Relevant equations

    3. 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):
  2. jcsd
  3. Feb 28, 2017 #2


    User Avatar
    Gold Member

    Just as a side note, you should learn to use the code tags so that your code is readable.
  4. Feb 28, 2017 #3


    User Avatar
    Science Advisor

    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.
  5. Feb 28, 2017 #4
    How do I do this?

    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.
  6. Feb 28, 2017 #5


    User Avatar
    Gold Member

Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted