User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 397,767 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,470 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 2776 | Replies: 8
Reply
Join Date: Dec 2004
Location: Minnesota, United States
Posts: 29
Reputation: MasashiMikamida is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
MasashiMikamida's Avatar
MasashiMikamida MasashiMikamida is offline Offline
Light Poster

HELP! Illegal Character Constant in a Console Application

  #1  
Dec 16th, 2004
I'm not incredibly skilled with C/C++, but I'm writing a C++ Win32 Console Application that inputs an atomic formula, and outputs the molar mass. Everything should work fine, but in my if structures where the user input is the atomic symbol for Uuq, or any element that has 3 letters for a symbol, It gives me and error. The input type for the atomic symbol is char. My arguments look like so:

char amtnam1, Uuq;
long int Uuq;
float Uuq=5.68;

/*1*/if (amtnam1='Uuq') atom1=Uuq;

I heard that I might have to change from *-Bit to 16-Bit or something....

PLEASE HELP ME!!
Last edited by alc6379 : Dec 16th, 2004 at 5:17 pm.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2004
Location: Devon - UK
Posts: 420
Reputation: 1o0oBhP is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 6
1o0oBhP's Avatar
1o0oBhP 1o0oBhP is offline Offline
Posting Pro in Training

Re: HELP! Illegal Character Constant in a Console Application

  #2  
Dec 16th, 2004
can you post what code you have so far??? im slightly confused as to exactly what you mean (and ive studied chemistry...) do you mean inputing an atomic symbol?
http://sales.carina-e.com

no www
no nonsense

coming soon to a pc near you! :cool:
Reply With Quote  
Join Date: Apr 2004
Posts: 3,462
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 16
Solved Threads: 138
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: HELP! Illegal Character Constant in a Console Application

  #3  
Dec 16th, 2004
Originally Posted by MasashiMikamida
/*1*/if (amtnam1='Uuq') atom1=Uuq;
The message is telling you that 'Uuq' is an illegal character constant. It appears to be an attempt to be a string constant ("Uuq"), but you don't compare C-style strings with ==; instead look into strcmp().
Reply With Quote  
Join Date: Dec 2004
Location: Minnesota, United States
Posts: 29
Reputation: MasashiMikamida is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
MasashiMikamida's Avatar
MasashiMikamida MasashiMikamida is offline Offline
Light Poster

Re: HELP! Illegal Character Constant in a Console Application

  #4  
Dec 17th, 2004
Yeah; the program is designed so that you input the atomic symbol, then the amount of that atom in a formula. Then the program outputs the total atomic mass, and you have the option of inputting X number of grams with an output of how many moles it is equal to (ie: 500g of H2=28mol). Of course that not the real measurement, but you get the idea...
Reply With Quote  
Join Date: Dec 2004
Location: Minnesota, United States
Posts: 29
Reputation: MasashiMikamida is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
MasashiMikamida's Avatar
MasashiMikamida MasashiMikamida is offline Offline
Light Poster

Re: HELP! Illegal Character Constant in a Console Application

  #5  
Dec 17th, 2004
Here is a larger sample of code that I'm working with. The entire program is much too large to post...

#include <iostream.h>
#include <stdlib.h> //For Clear Screen
#include <io.h> //For Sleep

long int amtnum1, amtnum2; //user input of the amount of an atom
int atom1, atom2; //used for calculating moles
char amtnam1, amtnam2; //user input of the atomic symbol

float H=1.00797; //Atomic Mass of Hydrogen
float He=4.0026; //Atomic Mass of Helium
float Uuq=285; //Atomic Mass of Ununquadrium

void atomic_formula(); //Function gets user input of atomic formula
void input_recognition(); //Function assigns atomic mass to atom1, atom2 variables
void ans_display(); //Function calculates and outputs molar mass

int main()
{
cout<<"Molar Mass Converter"<<endl;
sleep(5); //Pauses program
system("cls"); //clears screen

atomic_formula();

}

void atomic_formula()
{
cout<<"What is the atomic symbol of the first element?"<<endl;
cin>>amtnam1;
cout<<"What is the total amount of this element?"<<endl;
cin>>amtnum1;

cout>>"What is the stomic symbol of the second element?"<<endl;
cin>>amtnam2;
cout<<"What is the total amount of this element?"<<endl;
cin>>amtnum2;

input_recognition();

}

void input_recognition()
{
if (amtnam1='H') atom1=H;
if (amtnam1='He') atom1=He;
if (amtnam1='Uuq) atom1=Uuq;

if (amtnam2='H') atom2=H;
if (amtnam2='He') atom2=He;
if (amtnam2='Uuq) atom2=Uuq;

else atomic_formula();

ans_display();
}

void ans_display()
{
//math formula for displaying mass in moles and/or mass of a single mole
}


I wrote the program in Metrowerks Codewarrior IDE Studio 2004. Again, it's a Win32, C++, Console Application. I made a hoard of if structures so that even the most unexperienced C++ programmer could figure it out. That, and it just seemed more simple for me to write, and to change or add new elements if I need to.

The program works 9if I make the Uuq, etc, elements comments. Then it works just fine. But it needs to be able to have a three character input and working if argument. I've asked some friends, who used to teach college-level programming, what they think and they didn't see anything wrong with the code either (except that it would've been easier to use an array or matrix or something to replae the 16,000 lines of if structures!).
Reply With Quote  
Join Date: Apr 2004
Posts: 3,462
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 16
Solved Threads: 138
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: HELP! Illegal Character Constant in a Console Application

  #6  
Dec 17th, 2004
Originally Posted by MasashiMikamida
The program works 9if I make the Uuq, etc, elements comments. Then it works just fine. But it needs to be able to have a three character input and working if argument. I've asked some friends, who used to teach college-level programming, what they think and they didn't see anything wrong with the code either (except that it would've been easier to use an array or matrix or something to replae the 16,000 lines of if structures!).
That's scary.

Originally Posted by Dave Sinkula
The message is telling you that 'Uuq' is an illegal character constant. It appears to be an attempt to be a string constant ("Uuq"), but you don't compare C-style strings with ==; instead look into strcmp().
You need to learn strings.
Reply With Quote  
Join Date: Dec 2004
Location: Minnesota, United States
Posts: 29
Reputation: MasashiMikamida is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
MasashiMikamida's Avatar
MasashiMikamida MasashiMikamida is offline Offline
Light Poster

Re: HELP! Illegal Character Constant in a Console Application

  #7  
Dec 17th, 2004
Yeah. That's what everyone I've showed the program to has said; "Why aren't you using strings?!"

In hindsight it probably would have been 16,000 lines more efficent , but at the time, the use of strings never really ouccured to me. I just whipped it up in about 5 hours. I had it in mind that if i needed to add a new element, all i'd have to do is copy-paste the series of if structures, and add a float variable.

But I just don't get it; why won't in accept 3 character input? Why? Iworks with one and two, but not three...
Reply With Quote  
Join Date: Apr 2004
Posts: 3,462
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 16
Solved Threads: 138
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: HELP! Illegal Character Constant in a Console Application

  #8  
Dec 17th, 2004
Originally Posted by MasashiMikamida
In hindsight it probably would have been 16,000 lines more efficent , but at the time, the use of strings never really ouccured to me.
You wrote 16,000 lines of code without trying to compile?

Originally Posted by MasashiMikamida
But I just don't get it; why won't in accept 3 character input? Why? Iworks with one and two, but not three...
Because 3 characters is not a single character; it is an attempt to be a string.
Reply With Quote  
Join Date: Dec 2004
Location: Devon - UK
Posts: 420
Reputation: 1o0oBhP is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 6
1o0oBhP's Avatar
1o0oBhP 1o0oBhP is offline Offline
Posting Pro in Training

Re: HELP! Illegal Character Constant in a Console Application

  #9  
Dec 17th, 2004
heres an idea. Create an array of elemts. this allows you to simplify it much more.

Just a quick thought:

struct element
{
    char *name; // eg "carbon"
    char *symbol; // eg "C" this is what you compare for
    int atomic_number;
    float atomic_mass;
};

then when you cin an input string you can use strcmp(string1, string2) in a loop to go through all the elements untill it finds a match
element periodic_table[20]; // store up to calcium

char *input; // you CANT use char for your symbol!!!! this is because some elements have MORE THAN ONE CHARACTER (one of the problems)
cout << "enter symbol";
cin >> input; // input has a symbol

for(int i = 0; i < 20; i++)
{
    if(strcmp(input, periodic_table[i].symbol))
    {
        // you have a match
        cout << "Element: " << periodic_table[i].name << "\n";
        cout << "Atomic Number: " << periodic_table[i].atomic_number << "\n";
        cout << "RAM: " << periodic_table[i].atomic_mass << "\n";

        // do any other processing here (mole/mass/ram calcs)
    }
}
hope this gives you a few ideas
http://sales.carina-e.com

no www
no nonsense

coming soon to a pc near you! :cool:
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 4:13 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC