#include <iostream>
#include <cstdio>
#include<string>
using namespace std;
//I'm new to c++ and I feel incredibly dumb for this but this program isn't working whenever I run it it prints "eight" and "nine" to the console but instead of also printing even and odd it shows this 1�I��^H��H���PTI���
@.
//This program is meant to take input numbers a and b then count from a to and if it is less than 10 output the number as it's spelled. If it's greater output whether it's even or odd.
Are you required to use the for loop to count the numbers from a to b? Do you have the full instructions of your program or is it only what you mentioned above?
#include <iostream>
#include <string>
#include <cmath> // To use the abs() operator.
int main()
{
int a{ 0 }, b{ 0 };
std::string num_spell[] = { "one \n", "two \n", "three \n",
"four \n", "five \n", "six \n", "seven \n",
"eight \n", "nine \n" };
std::cout << "Input two numbers\n";
std::cin >> a >> b;
// Count from a to b.
int count = a - b;
count = abs(count); //Use the absolute value and defined the variable to determine the count between 2 numbers.
// If it is less than 10, then output the number as it's spelled.
if (count < 10)
std::cout << num_spell[count - 1];
// If it is greater, then output wheter it's even or odd.
elseif (count > 9)
{
if (count % 2 == 0)
std::cout << "Even\n";
else
std::cout << "Odd\n";
}
return 0;
}
instead of also printing even and odd it shows this 1�I��^H��H���PTI���
@.
That's an out-of-bounds array access. Array num_spell[] has nine elements. The valid subscripts for that array are 0 to 8. Your code attempts to use num_spell[9] which is the tenth element - it is outside the array.