CodeGuru Forums -
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic Newsletters VB Forums Developer.com


Newest CodeGuru.com Articles:

  • Installing SQL Server 2008
  • Writing UDFs for Firebird Embedded SQL Server
  • [Updated] Shutdown Manager
  • Building Windows Azure Cloud Service Applications with Azure Storage and the Azure SDK

  • Search CodeGuru:
     



    Go Back   CodeGuru Forums > Visual C++ & C++ Programming > Visual C++ Programming
    FAQ Members List Calendar Search Today's Posts Mark Forums Read

    Visual C++ Programming Ask questions about Windows programming with Visual C++ and help others by answering their questions.

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1    
    Old November 7th, 2009, 12:16 AM
    turbosupra turbosupra is offline
    Junior Member
     
    Join Date: Nov 2009
    Posts: 13
    turbosupra is an unknown quantity at this point (<10)
    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
    
     }
    Reply With Quote
      #2    
    Old November 7th, 2009, 05:41 AM
    VictorN's Avatar
    VictorN VictorN is offline
    Elite Member
    Power Poster
     
    Join Date: Jan 2003
    Location: Hannover, Germany
    Posts: 9,893
    VictorN has a reputation beyond repute (3000+)VictorN has a reputation beyond repute (3000+)VictorN has a reputation beyond repute (3000+)VictorN has a reputation beyond repute (3000+)VictorN has a reputation beyond repute (3000+)VictorN has a reputation beyond repute (3000+)VictorN has a reputation beyond repute (3000+)VictorN has a reputation beyond repute (3000+)VictorN has a reputation beyond repute (3000+)VictorN has a reputation beyond repute (3000+)VictorN has a reputation beyond repute (3000+)
    Re: Need help with homework please ...

    In what line of your code do you get this error?
    __________________
    Victor Nijegorodov
    Reply With Quote
      #3    
    Old November 7th, 2009, 06:24 AM
    Rich2189 Rich2189 is offline
    Member +
     
    Join Date: Oct 2005
    Location: England
    Posts: 800
    Rich2189 is a glorious beacon of light (400+)Rich2189 is a glorious beacon of light (400+)Rich2189 is a glorious beacon of light (400+)Rich2189 is a glorious beacon of light (400+)Rich2189 is a glorious beacon of light (400+)
    Re: Need help with homework please ...

    Quote:
    Originally Posted by turbosupra View Post


    Code:
                int grade = 0;
    
    
    	 if (score >= 85 )
    	 {
    		 grade = "A";
    	 }
    
    
    
     }
    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~
    Reply With Quote
      #4    
    Old November 7th, 2009, 08:52 AM
    turbosupra turbosupra is offline
    Junior Member
     
    Join Date: Nov 2009
    Posts: 13
    turbosupra is an unknown quantity at this point (<10)
    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:
    Originally Posted by Rich2189 View Post
    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.
    Reply With Quote
      #5    
    Old November 7th, 2009, 09:09 AM
    hoxsiew hoxsiew is offline
    Senior Member
     
    Join Date: Feb 2005
    Posts: 1,189
    hoxsiew  is a jewel in the rough (300+)hoxsiew  is a jewel in the rough (300+)hoxsiew  is a jewel in the rough (300+)hoxsiew  is a jewel in the rough (300+)
    Re: Need help with homework please ...

    Code:
    grade = "A";
    Should be:

    Code:
    grade = 'A';
    In C++, the compiler treats any thing between two double quotes as a character array with a NULL terminator so "A" is literally a char[2]. Single quotes on the other hand tell the compiler to treat the value as a single char (the value of which is the ASCII value of the character) which is an integral value and can be used in comparisons and assignments.
    Reply With Quote
      #6    
    Old November 7th, 2009, 10:52 AM
    turbosupra turbosupra is offline
    Junior Member
     
    Join Date: Nov 2009
    Posts: 13
    turbosupra is an unknown quantity at this point (<10)
    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:
    Originally Posted by hoxsiew View Post
    Code:
    grade = "A";
    Should be:

    Code:
    grade = 'A';
    In C++, the compiler treats any thing between two double quotes as a character array with a NULL terminator so "A" is literally a char[2]. Single quotes on the other hand tell the compiler to treat the value as a single char (the value of which is the ASCII value of the character) which is an integral value and can be used in comparisons and assignments.
    Reply With Quote
    Reply

    Bookmarks
    Go Back   CodeGuru Forums > Visual C++ & C++ Programming > Visual C++ Programming


    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is On
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 02:05 AM.



    Acceptable Use Policy

    internet.comMediabistrojusttechjobs.comGraphics.com

    WebMediaBrands Corporate Info


    Advertise | Newsletters | Feedback | Submit News

    Legal Notices | Licensing | Permissions | Privacy Policy


    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
    Copyright WebMediaBrands Inc. 2002-2009