Do while loop with stricmp

hi, i'm trying to make a molar mass calculator where you have to input the name/symbol of the element and the multiplier to compute it. my problem is, if the user enters the wrong element (element does not exist or typo), i want the program to keep asking until he/she gets it right.. here's what i did so far (haven't inputted all the elements yet)
:



#include <iostream>
#include <conio.h>
#include <windows.h>
#include <cmath>
#include <string>

using namespace std;
int main()

{

double mm1=0, mm2=0, mm3=0, mmtot=0, mmtoteng=0;
int option=0, mole1=0, mole2=0, mole3=0;
string name;
string sym1;
string sym2;
string sym3;
char yesno;



cout<<"To start program, please enter your name: ";
cin>>name;

getch();
system("cls");




cout<<"Hello, "<<name<<"! Welcome to XYIENTIST MOLAR MASS CALCULATOR.\n\n";
cout<<"Compute for: \n\n[1]Element\n[2]Polyatomic Molecule\n[3]Two-element Compound\n[4]Three-element compound";
cout<<"\nEnter option: ";
cin>>option;


getch();
system("cls");


if (option==3)

{
do
{


cout<<"Enter first element name or symbol:";
char element1[20];
cin>>element1;


cout<<"Enter number of moles of element1: ";
cin>>mole1;


if ((stricmp ("Hydrogen",element1) == 0) || (stricmp ("H",element1) == 0))
{
mm1=1.008;
sym1='H';

}

else
{
cout<<"\nNo such element exists!";
}


}

while (stricmp(element1,element1)!=0);




cout<<"Enter 2nd element name or symbol:";
char element2[20];
cin>>element2;
cout<<"Enter number of moles of element2: ";
cin>>mole2;

if ((stricmp ("Carbon",element2) == 0) || (stricmp ("C",element2) == 0))
{
mm2=12.01;
sym2='C';

}

cout<<"Enter 3rd element name or symbol:";
char element3[20];
cin>>element3;
cout<<"Enter number of moles of element3: ";
cin>>mole3;


if ((stricmp ("Oxygen",element3) == 0) || (stricmp ("O",element2) == 0))
{
mm3=16.00;
sym3='O';

}



cout<<"\n\nCompound is "<<sym1<<mole1<<sym2<<mole2<<sym3<<mole3<<" .";
mmtot=(mm1*mole1)+(mm2*mole2)+(mm3*mole3);
cout<<"\nMolar mass is:"<<mmtot;



}







else

cout<<"Invalid!";


return 0;

}


how do i loop the program? thanks.
I'm not too fancy with stricmp.

In your final while(), you compare Element1 to Element1, so this will always equal zero. You want to compare "Hydrogen" to Element1 (and also "H").

Secondly, the whole point of your do while is to valid the user input. You don't want to set variables inside it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
do
{
cout<<"Enter first element name or symbol:";
char element1[20];
cin>>element1;

cout<<"Enter number of moles of element1: ";
cin>>mole1;


if ((stricmp ("Hydrogen",element1) != 0) || (stricmp ("H",element1) != 0))
{
 cout<<"\nNo such element exists!";
}

while (stricmp ("Hydrogen",element1) != 0) || stricmp ("H",element1) != 0);

mm1=1.008;
sym1='H';


I would also recommend making functions in your program. Your code is going to get very repetitive.
Last edited on
we've only been taught the very basics of c++.
if it's not too much, may i ask how functions are used?

thanks for the reply.
A function is a complete lump of code; you feed it zero or more "parameters" and you get back zero or one parameter.

For example, a function that had this prototype:
int add(int a, int b);
is a function named add that takes two parameters (they're in brackets, on the right - two int values) and returns (gives you back) an int.

Here are some more examples:

void add(float a, float b);
is a function named add that takes two float values and does not return anything (void indicates that there is nothing given back).

The parameters you pass it, and what you get back, may be of any built in type (such as int, char, double, float, string, etc) or any struct/class type you create yourself.

monster eatAllGrass ( typhoon a, eggsOnToast b);
is a function named eatAllGrass that takes in an object of type typhoon and an object of type eggsOnToast, and gives you back an object of type monster.


int stricmp( const char* s1, const char* s2 );
stricmp is a function that takes in a const char* (i.e. a pointer to a const char), and another const char*, and gives back an int.

So, this would not work:
string x = stricmp( "a", "b" );
because you've trying to assign the value you get back, an int, to a string.
This would work:
int x = stricmp( "a", "b" );

So, where you see
stricmp ("Hydrogen",element1) in the code, you know that the function will be executed, and an int given back.

The code
while (stricmp ("Hydrogen",element1) != 0) || stricmp ("H",element1) != 0);
could be read as
while (someInteger != 0 || someOtherInteger != 0)
where someInteger is the returned value from
stricmp ("Hydrogen",element1)
and someOtherInteger is the returned value from
stricmp ("H",element1)


Enough?
Last edited on
since the coffee is still hot :)

Functions: http://www.cplusplus.com/doc/tutorial/functions/

A function is a piece of code that you can call over and over again. stricmp is a function. You can make your own too, they save a lot of time. It is hard to give a useful example without getting too complicated. The first functions we made are for validation. We had code that looked like this:
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
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
using namspace std;

int getInt(); // A function prototype, tells the rest of the program
          // that this function exists.  Has a return type of integer.

int main (){
  int x, y, z;
  
  x = getInt(); // Calling our function, it returns the integer, and stores it in x;
  y = getInt(); // Again, saved typing time already.

  z = x + y;
  
  cout << x << " + " << y << " = " << z ;

  //  This is preferable to system("pause");
  cout << "\n\nDone.  Press Enter to Quit.";
  cin.get();

  return 0;
}

int getInt() // The actual function
{
  double x;
  do
  {
     cout << "Please enter an Integer: ";
     cin >> x;
     if (static_cast<int>(x) != x)  // If double x isn't the same when it is casted as an int.
                                    // Could also be written as ( int(x) != x).
        cout << "That is not an integer\n";
  }
  while (static_cast<int>(x) != x);
  return x; // This will give you a "possible loss of data warning",
            //but you have already ensured that the value stored in x is an integer number.
}


Sorry if you don't know what the static_cast<int> is. I would guess you do if you are using <string> already?



Last edited on
Topic archived. No new replies allowed.