REVIEW THIS CODE......

1
2
3
4
5
6
7
8
9
std::cout<<"\nEnter Type Of The Account (C/S) : ";
	type=getche();
	type=toupper(type);
	if(strcmp(type,'C')||strcmp(type,'S'))  {}
	else
    {
        cout<<"\n Incorrect Account Type, Enter Again";
        goto again;
    }


GIVES AN ERROR LIKE THIS:
invalid conversion from 'char' to 'const char*' [-fpermissive]

WHAT MAY BE THE REASON?
The variable named type is a single char, not an array of char.
The std::strcmp() function is for arrays of characters.

You can compare single characters by using the equality operator:

1
2
3
4
5
6
7
8
if (type == 'C' || type == 'S')
{
    // ...
}
else
{
    // ...
}


Also, as a C++ programmer, you should use std::string instead of char arrays.
http://www.cplusplus.com/reference/string/string/
thanks! it worked.

i couldn't understand the difference b/w char and std::string, can you please explain?
std::string is how you should store text as a C++ programmer.
char arrays are the way you store text as a C programmer.

You should use std::string because it is more powerful than char arrays, and it is easier to use.

char arrays are inherited from C and can be harder to use.
(For example you need strcmp() to compare them, and strlen() to get their length.)

Example for std::string usage:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <string>

int main()
{
    std::string text1("Cat");
    std::string text2 = "Dolphin"; // alternative declaration

    std::cout << "The string \"" << text1 << "\" contains " << text1.length() << " chars.\n";

    if (text1 != text2) // can directly compare
        std::cout << '"' << text1 << "\" and \"" << text2 << "\" are not the same.\n";
    else
        std::cout << '"' << text1 << "\" and \"" << text2 << "\" are the same!\n";

    std::string reversed_text2(text2.rbegin(), text2.rend());

    std::cout << "The reverse of \"" << text2 << "\" is \"" << reversed_text2 << "\".\n";

    // can easily build new strings by concatenating existing ones
    // no worry about how many chars can text3 hold (it is dynamic)
    std::string text3 = text1 + " and " + text2 + " are not good friends.";

    std::cout << text3 << '\n';
    std::cout << "The above text contains " << text3.length() << " chars.\n";
    std::cout << std::endl;
}


The string "Cat" contains 3 chars.
"Cat" and "Dolphin" are not the same.
The reverse of "Dolphin" is "nihploD".
Cat and Dolphin are not good friends.
The above text contains 37 chars.

Last edited on
wow thats very nice, thanks now onwards i'll use this only.
thanks buddy
Topic archived. No new replies allowed.