Write a program to assign to, and later output to the screen, a string representing a value. The value, in the range of [0, 9], is read, from a file. For example, a value of 4 causes the string to be assigned four.
As it is, When I've written a "4" to the file Value.txt located in the project directory, I get "fournine" printed to the screen. Why?
Also, I realize I have thus far ignored the string part of this question. I though setting it up this way first might help me better understand how to go about building the program. I didn't, hence me posting here. PLEASE, I NEED HELP.
// This program is inteded to assign an alphabetic value to its corresponding numeric score and write the result to a file
#include <iostream>
#include <fstream>
usingnamespace std;
int score; // Numeric value
char grade; // Alphabetic value assigned to a given numeric value
int main() {
cout << "Please enter a score: "; // Prompts user to enter a score
cin >> score;
cout << endl;
// If-else statement for calculating a score's corresponding grade
if (score >=80) {
grade = 'A'; }
elseif (score >=70) {
grade = 'B'; }
elseif (score >=60) {
grade = 'C'; }
elseif (score >=50) {
grade = 'D'; }
else {
grade = 'F'; }
ofstream outData;
outData.open ("Grade.txt"); // Creates a file named "Grade.txt"
outData << "The grade given for a score of " << score << " is " << grade << endl; // Writes the users grade to the file "Grade.txt"
outData.close();
cout << "Your corresponding grade was written to the file Grade.txt" << endl; // Informs the user where they can find their grade
cout << endl;
system ("PAUSE");
return 0;
YOU ARE NOT COPYING THE CODE I PASTED. IT'S NOT THE SAME DIFFERENCE. You're putting a semicolon after your condition. Your else if isn't checking anything, the code is just being executed regardless. Take out the semicolon in
I remembering learning in a lecture something about strings with only one character needing 'single quotes' as opposed to a string with multiple characters using "double quotes". Clearly whatever I thought I was thinking of was wrong. Do you have any idea what my proff might actually have been referring to?
You know, I have to admit, learning to code C++ is difficult. It's literally like learning a second language. However, learning, understanding, and executing its concepts can be quite rewarding. Thanks again, gents.
so, that's a different way to solve. typing if else for every number maybe tiresome. you can make it much shorter by string s[4] = {"one","two","three","four"}; way.