0

I'm using turbo c++ for my school project. I know it's very old and I should use code blocks or MVC++, but I just use it for simple program for school. Okay so my problem is that I cannot print out char arrays. Here is my simple code

#include <iostream.h>
#include <conio.h>

class abc
{
    private:
            int rno,m1,m2,tot;
            char sname[10],grade;
    public:
            void getinput()
            {
                cout<<"Enter roll no:"<<endl;
                cin>>rno;
                cout<<"\nEnter mark 1:"<<endl;
                cin>>m1;
                cout<<"\nEnter mark 2:"<<endl;
                cin>>m2;
                cout<<"\nEnter student name:"<<endl;
                cin>>sname;
                cout<<endl;
                // Getting the returns.
                //tot = gettotal();
                //grade = getgrade();
            }

            void gettotal()
            {
                tot = m1+m2;
                // Returning the total to getinput's "tot" part.
                //return (tot);
            }

            void getgrade()
            {
                if(tot >= 150)
                {
                    grade = 'A';
                }
                else if(tot >= 100)
                {
                    grade = 'B';
                }
                else
                {
                    grade = 'C';
                }
                // Returning the total to getinput's "grade" part.
                //return (grade);
            }

            void display()
            {
                cout<<sname<<"'s total grade ranks: "<<grade;
            }
 };


 void main()
 {
    abc a;
    a.getinput();
    a.gettotal();
    a.getgrade();
    a.display();
 }

As you can see, it asks for marks 1 and marks 2 and then calculate it and prints 'A','B' and 'C' depending on marks.

Okay so what I want is to print 'First','Second' and 'Third' instead of A,B,C.

Can someone help me on this please.

Thanks.

5
  • Have you tried? Because unlike your title, it looks like you can print out sname no problem? Commented Jan 14, 2013 at 10:01
  • I can print sname without any problem but not grade. Don't know what's the problem. It's just that I am having problem in defining grade to "First","Second" and "Third". Commented Jan 14, 2013 at 10:11
  • Try the method in my answer Commented Jan 14, 2013 at 10:31
  • 1
    How would your code print 'First', 'Second', 'Third' when that is nowhere in your code? Commented Jan 14, 2013 at 11:12
  • my suggestion would be to use std::string instead of char*. Commented Jan 14, 2013 at 11:19

3 Answers 3

1

Try this approach:

enum Grade{
    A,
    B,
    C,
};

const char * GetGradeStr(Grade grade){
    switch(grade){
    case A:return "First";
    case B:return "Second";
    case C:return "Third";
    }
}

char sname[10];
Grade grade;

...
    grade = A;

...
    cout<<sname<<"'s total grade ranks: "<<GetGradeStr(grade);

This makes grade into an enum, which means only the 3 values are valid values, and then returns the correct string as a "String Literal"

Sign up to request clarification or add additional context in comments.

Comments

1

Maybe I can add something, to make things even clearer for you. You need to understand the difference between char arrays and pointers. A pointer is just a variable which points to some area in memory. You can malloc memory and store the address into a char* - this will behave similar to a char array. But it's not the same.

A char array (and any array in C/C++) has to have a known size at compile time. The compiler will then "allocate" this memory statically, as opposed to allocating at runtime via malloc or new. This array can now be implicitly converted to a char* - this is what happens when you print it, for example. Look at the following code:

char* foo = "Hello world";

What this actually does is something like this:

// Compiler generated "global constant",
// somewhere in your executable
const char fooString[] = "Hello world";

char* foo = fooString;

For every string literal you use in your code, the compiler will generate such a "global constant". By assigning to a char pointer, the pointer now points to that constant's memory address, therefore it's possible to print it, index it (foo[2]), copy it, etc.

If you're using char arrays, the compiler will also allocate space at compile time. The char sname[10] for example will cause every object of class abc to contain memory for 10 chars (in addition to the other members etc.) Now when you reference sname in your code, it's type will be char[10], but it can be (implicitly) converted to achar*` pointing to the first char in the array. Now let's have a look at the following code:

grade = "First";
grade = "Second";
grade = "Third";

If we remember that string literals are converted to global constants, it actually is something like the following:

const char[] str1 = "First";
const char[] str2 = "Second";
const char[] str3 = "Third";

grade = str1;
grade = str2;
grade = str3;

As you might guess now, this will work nicely if grade is a char pointer: It will just point to one of the constant string literals, and can then be used to print it etc. But if grade is an array, the compiler sees an assignment from char* to char[] - which is not possible. But it is possible using strcpy - why? Because strcpy takes to char pointers as arguments. Thus, the compiler will implicitly convert the char array grade into a pointer to it's first element, and pass this to strcpy.

Comments

0

You have defined grade to be a char and not a char * . Please update that to become char * then you can assign those values like 'First' etc

Edit

#include <iostream.h>
#include <conio.h>

class abc
{
    private:
            int rno,m1,m2,tot;
            char sname[10];
            char * grade;
    public:
            void getinput()
            {
                cout<<"Enter roll no:"<<endl;
                cin>>rno;
                cout<<"\nEnter mark 1:"<<endl;
                cin>>m1;
                cout<<"\nEnter mark 2:"<<endl;
                cin>>m2;
                cout<<"\nEnter student name:"<<endl;
                cin>>sname;
                cout<<endl;
                // Getting the returns.
                //tot = gettotal();
                //grade = getgrade();
            }

            void gettotal()
            {
                tot = m1+m2;
                // Returning the total to getinput's "tot" part.
                //return (tot);
            }

            void getgrade()
            {
                if(tot >= 150)
                {
                    grade = "First";
                }
                else if(tot >= 100)
                {
                    grade = "Second";
                }
                else
                {
                    grade = "Third";
                }
                // Returning the total to getinput's "grade" part.
                //return (grade);
            }

            void display()
            {
                cout<<sname<<"'s total grade ranks: "<<grade;
            }
 };


 int main()
 {
    abc a;
    a.getinput();
    a.gettotal();
    a.getgrade();
    a.display();
 }

8 Comments

Sorry but I'm still at learning process in c++. I have already tried char grade[10] and then to define char garde[] = "First"; . Is that right? I'm sure I have done some stupid mistake there.
Thanks for the code, but when I compile it, it gave 3 errors telling "Lvalue required ... " on the grade lines.
Using a char array instead of a pointer, it won't be possible to simply assign string literals. Instead, something like strcpy would be required. Could you remove that suggestion, or clarify it?
@Vicky Yes that was a mistake, it was updated in last revision
@DotNetZeZo Glad it helped. If your instructor would not allow you to use char * for now then use a char array and then use strcpy function like suggested by Vicky and lethal-guitar
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.