•
•
•
•
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
![]() |
•
•
Join Date: Dec 2004
Location: Minnesota, United States
Posts: 29
Reputation:
Rep Power: 4
Solved Threads: 0
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!!
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.
•
•
Join Date: Dec 2004
Location: Minnesota, United States
Posts: 29
Reputation:
Rep Power: 4
Solved Threads: 0
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...
•
•
Join Date: Dec 2004
Location: Minnesota, United States
Posts: 29
Reputation:
Rep Power: 4
Solved Threads: 0
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!).
#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!).
•
•
•
•
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!).
•
•
•
•
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().
•
•
Join Date: Dec 2004
Location: Minnesota, United States
Posts: 29
Reputation:
Rep Power: 4
Solved Threads: 0
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. 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...
•
•
•
•
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.
•
•
•
•
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...
heres an idea. Create an array of elemts. this allows you to simplify it much more.
Just a quick thought:
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
hope this gives you a few ideas
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)
}
}
http://sales.carina-e.com
no www
no nonsense
coming soon to a pc near you! :cool:
no www
no nonsense
coming soon to a pc near you! :cool:
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
Other Threads in the C++ Forum
- Previous Thread: class polynomial
- Next Thread: CAn any1 tell me watz wrong in da code


do you mean inputing an atomic symbol?

Linear Mode