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!!

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?

/*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().

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...

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!).

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.

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.

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...

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?

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.