Troubleshooting C++ Program: Invalid Conversion Error

  • Thread starter Thread starter Zurtex
  • Start date Start date
  • Tags Tags
    C++
Click For Summary

Homework Help Overview

The discussion revolves around a C++ programming issue related to type conversion errors, specifically when attempting to assign a string to a character variable. The original poster is working on a program that sorts numbers and is encountering an invalid conversion error when trying to assign a string literal to a char type.

Discussion Character

  • Conceptual clarification, Assumption checking

Approaches and Questions Raised

  • Participants discuss the distinction between single quotes and double quotes in C++, noting that single quotes are used for characters while double quotes denote strings. There is an exploration of the implications of this distinction on the error encountered.

Discussion Status

Several participants have provided insights regarding the syntax error, emphasizing the need to use single quotes for character assignments. The discussion appears to be productive, with participants clarifying the underlying concepts without reaching a definitive conclusion.

Contextual Notes

The original poster has not provided the complete code, which may limit the depth of the discussion. There is an implicit understanding that the assignment of a string to a char variable is incorrect, but the specifics of the program's requirements are not fully outlined.

Zurtex
Science Advisor
Homework Helper
Messages
1,118
Reaction score
1
Just been introduced to the world of C++ as I'm taking a course on it in my maths degree. We are asked to write a simple program that takes in 3 numbers, sorts them in terms of order and then outputs them.

Not going to type my full program as that would probably be a waste of your time but here are the two important lines I am stuck on:

char qualifier1;
...
qualifier1 = "=";

And I'm getting the error when I try to compile this:

invalid conversion from `const char*' to `char'

Can anyone help me please, I'm not sure what this means and I am struggling to fix it.
 
Physics news on Phys.org
i believe the correct syntax for a single char is ' ' not " "...if i remember correctly " " denotes strings...and thus your error is saying
cannot convert const char* "=" (string) to a char (single char qualifier)
 
Using double quotes ("") in C and C++ returns a constant character pointer which points to a null-terminated string in the static section of memory. So if you write char* ptr = "dog"; you get ptr pointing to a character array consisting of 'd', 'o', 'g', and '\0', the null terminator.

What you're looking to do is use single quotes. When you type "=", you get the C-string "=". What you want to do is '=', with single quotes. This returns a plain character.

Edit: whoops too late
 
neurocomp2003 said:
i believe the correct syntax for a single char is ' ' not " "...if i remember correctly " " denotes strings...and thus your error is saying
cannot convert const char* "=" (string) to a char (single char qualifier)
Thanks :biggrin:
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 12 ·
Replies
12
Views
4K
  • · Replies 23 ·
Replies
23
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 14 ·
Replies
14
Views
5K
  • · Replies 7 ·
Replies
7
Views
3K
Replies
10
Views
2K
  • · Replies 14 ·
Replies
14
Views
35K