| CodeGuru Home | VC++ / MFC / C++ | .NET / C# | Visual Basic | Newsletters | VB Forums | Developer.com |
|
|||||||
| Visual C++ Programming Ask questions about Windows programming with Visual C++ and help others by answering their questions. |
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Need help with homework please ...
My syntax is wrong, and I can't figure out why. I'm very new to c++ and this is a very basic program, but I can't get my variables set right and I keep getting the error
"error C2440: '=' : cannot convert from 'const char [2]' to 'const char' 1> There is no context in which this conversion is possible" when I try and debug it. Help would be appreciated. Also, what are the appropriate variable declarations, for different types of data? Thanks for reading. Code:
// module3.cpp : Defines the entry point for the console application.
//
/*
If (score >= 85) then
Set grade = �A� Else
If (score >= 75) then
Set grade = �B�
Else
If (score >= 65) then
Set grade = �C�
Else
If (score >= 55) then
Set grade = �D�
Else
Set grade = �F�
End if // score >= 55
End if // score >= 65
End if // score >= 75
End if // score >= 85
Your assignment is to convert this pseudocode into a C++ program. Use the following template for your program and remember to maintain all the blank lines, spaces, and general alignment. Then replace the areas that have been highlighted in yellow with your code. Do not change any of the other code.
Code Template for Exercise 1 */
/****************************************************/
/* File: name of your file with the source code */
/* */
/* Created by: give your name */
/* Date: give the date */
/* */
/* Program to determine course grade */
/* */
/* Inputs: (keyboard) */
/* 1. Float - weighted total points (<= 100) */
/* */
/* Output: */
/* letter grade using pseudocode grading policy */
/* */
/* Algorithm: Comparisons using if-then-else */
/* */
/****************************************************/
#include <iostream>
using namespace std ;
int main()
{
//Declare score and grade appropriately as variables.
float score;
int grade;
// read in total score
cout << endl ;
cout << "Enter total score (float, must be <= 100) : " ;
cin >> score ;
if (score >= 85 )
{
grade = "A";
}
/*
if (score >= "85")
grade = "A";
if (score >= "75" + <= "84")
grade = "B";
if (score >= "65" + <= "74")
grade = "C";
if (score >= "55" + <= "64")
grade = "D";
if (score <= "54")
grade = "Drop the class";
*/
// display the result
cout << endl ;
cout << "Your grade for class is: " << grade << endl ;
return (0); // terminate with success
}
|
|
#2
|
||||
|
||||
|
Re: Need help with homework please ...
In what line of your code do you get this error?
__________________
Victor Nijegorodov
|
|
#3
|
|||
|
|||
|
Re: Need help with homework please ...
I guess it is this section of code which is causing that particular error, you cannot store a string literal into an integer.
Try declaring grade as type char and assigning it character literals. The syntax in your commented out if statement is incorrect. There are plenty of on line tutorials which will discuss how to do this correctly.
__________________
Rich Visual Studio Express 2008 | .NET Framework 3.5 SP1 Windows Vista (x64) | Ubuntu Senbonzakura Kageyoshi~ |
|
#4
|
|||
|
|||
|
Re: Need help with homework please ...
If you mean declaring it as
char grade = 0; (is that the proper syntax) I've tried that, and for some reason that did not work either? Quote:
|
|
#5
|
|||
|
|||
|
Re: Need help with homework please ...
Code:
grade = "A"; Code:
grade = 'A'; |
|
#6
|
|||
|
|||
|
Re: Need help with homework please ...
And that makes perfect sense with the error message I was receiving ... wow I must have spent 2 hours screwing with that, over an apostrophe vs quote ... lol!
Thanks for the help, you're the man! (or woman!) Quote:
|
![]() |
| Bookmarks |
|
||||||
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|