• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

My First C++ Program

tetra20

DD
Joined
Jan 17, 2009
Messages
1,315
Solutions
4
Reaction score
323
Location
Egypt
Hi.
This is my first c++ program that i have done it is like 5 in 1 :) hope you like it and Give me advices to have a better programming experience Lets Start now

First Source--it is a little long
Code:
#include <iostream>

using namespace std;
float add (float n1 , float n2)
{
  return n1 + n2;
}
float sub (float n1 , float n2)
{
  return n1 - n2;
}
float div (float n1, float n2)
{
  return n1 / n2;
}
float multi (float n1, float n2)
{
  return n1 * n2;
}
  bool loop = true;
  char h;

void cal(){
  int choice;
  float n1;
  float n2;
cout << "What is your Choice\n 1-Add \n 2-Sub\n 3-division\n 4-multiply"<< endl;
cin>>choice;
if (choice == 1)
{
  cout<< "what's your first number?" << endl;
  cin>>n1;
  cout << "what's your second number?" << endl;
  cin>>n2;
  cout<< "Your Result is ";
  cout << add(n1, n2) << endl;
  printf("\n");
  cout << "(y to retry this test/n to go to start menu)?\n";
  cin >> h;
  if (h == 'y'){
  cal();
  cout << "--------------------------\n";
  }else{
  loop = true;
  }
}
else
  if(choice == 2)
{  cout<< "what's your first number?" << endl;
  cin>>n1;
  cout << "what's your second number?" << endl;
  cin>>n2;
  cout<< "Your Result is ";
  cout << sub(n1, n2) << endl;
  printf("\n");
  cout << "(y to retry this test/n to go to start menu)?\n";
  cin >> h;
  if (h == 'y'){
  cal();
  cout << "--------------------------\n";
  }else{
  loop = true;
  }
}
else
  if(choice == 3)
{
  cout<< "what's your first number?" << endl;
  cin>>n1;
  cout << "what's your second number?" << endl;
  cin>>n2;
  cout<< "Your Result is ";
  cout << div(n1, n2) << endl;
  printf("\n");
  cout << "(y to retry this test/n to go to start menu)?\n";
  cin >> h;
  if (h == 'y'){
  cal();
  cout << "--------------------------\n";
  }else{
  loop = true;
  }
}
else
  if(choice == 4)
{
  cout<< "what's your first number?" << endl;
  cin>>n1;
  cout << "what's your second number?" << endl;
  cin>>n2;
  cout<< "Your Result is ";
  cout << multi(n1, n2) << endl;
  printf("\n");
  cout << "(y to retry this test/n to go to start menu)?\n";
  cin >> h;
  if (h == 'y'){
  cal();
  cout << "--------------------------\n";
  }else{
  loop = true;
  }
}
  }
void food(){
  int f;
cout << "Choose Your Best Food\n-[1]Meat\n-[2]Ham\n-[3]Pizza\n-[4]Nothing" << endl;
  cin >> f;
  if(f == 1)
  {
  cout << "You Love Meat\n" << endl;
  }
  else
  if(f == 2)
  {
  cout << "You Love Ham\n" << endl;
  }
  else
  if(f == 3)
  {
  cout << "You Love Pizza\n" << endl;
  }
  else
  if(f == 4){
  cout << "You Love Nothing.Damn you \n";
  }
  else
  if(f > 3)
  {
  cout << "Invalid. Try Again\n" << endl;
  }
  cout << "(y to retry this test/n to go to start menu)?\n";
  cin >> h;
  if (h == 'y'){
  food();
  cout << "--------------------------\n";
  }else{
  loop = true;
  }

}
void person(){
  string a;
  string b;
  string c;
  string z;
  string l;
  cout << "What Is Your Name: ";
  cin >> a;
  cout << "Where are you from: ";
  cin >> b;
  cout << "Your Lovely Baby: ";
  cin >> c;
  cout << "Your Favourite Colour:";
  cin >> z;
  cout << "What is your Age: ";
  cin >> l;
  cout << "(y to retry this test/n to go to start menu)?\n";
  cin >> h;
  if (h== 'y'){
  person();
  cout << "--------------------------\n";
  }else{
  loop = true;
  }
  }
void price(){
  int q;
  int a = 0;
  int c;
  char h;
  float t = 0;

  cout << "Please Choose how much items you want to count their prices\n";
  cin >> q;
  cout << "Enter Prices\n";
  while (a < q){
  cin >> c;
  a++;
  t = t + c;
  }
  cout << "Total: " << t << "\n";
  cout << "(y to retry this test/n to go to start menu)?\n";
  cin >> h;

  if (h == 'y'){
  price();
  cout << "--------------------------\n";
  }else{
  loop = true;
  }
}

void dice(){
  bool loopa = true;
  int z = 0;
  int o = 0;
  while (loopa == true) {
  int x;
  int die1 = 0;
  int die2 = 0;
  int a;
  int c;
  int die3 = 0;
  int die4 = 0;
int f;
char h;
srand(time(0));
die1 = 1+(rand())%12;
die2 = 1+(rand())%12;
die3 = 1+(rand())%12;
die4 = 1+(rand())%12;
  cout << "To Start Playing Write 1 To Exit 2\n" << endl;
  cin >> x;
  cout << "--------\n";
  if (x == 1){
  cout << "Your Turn: \n";
  cout << "First Roll: " << die1 << "\n" << "Second Roll: " << die2 << endl;
  cout << "Total: " << die1 + die2 << endl;
  a = die1 + die2;
  cout << "----------\n";
  cout << "Computer Turn: " << endl;
  cout << "First Roll: " << die3 << "\n" << "Second Roll: " << die4  << endl;
  cout << "Total: " << die3 + die4 << endl;
  cout << "----------\n";
  c = die3 + die4;
  if(a > c){
  cout << "Gratzz You Won\n" << endl;
  cout << "(y to retry this test/n to go to start menu)?\n";
  z++;
  cout << "You: " << z << " - Com: " << o << "\n";
  cin >> h;
  cout << "\n\n";
  } else {
  cout << "Better Luck Next Time (Loser)." << endl;
  cout << "(y to retry this test/n to go to start menu)?\n";
  o++;
  cout << "You: " << z << " - Com: " << o << "\n";
  cin >> h;
  cout << "--------------------------\n";
  }
  if (h == 'y'){
  cout << "--------------------------\n";
  loopa = true;
  } else {
  loopa = false;
  cout << "--------------------------\n";
  }
  if (x == 2){
  cout << "Bye.Open Me Later" << endl;
  loop = false;
  }
  if(x >= 3){
  cout << "Please Choose Between 1-2\n" << endl;
  loop = true;
  }
  }
  }
  }



int main()
{
  int f;
  while (loop == true){
  cout << "Choose One Of Those To Start \n-[1]Food Test\n-[2]Personality Test\n-[3]Calculator\n-[4]Prices\n-[5]Dice\n";
  cin >> f;
  if(f == 1)
  food();
  else
  if(f == 2)
  person();
  else
  if(f == 3)
  cal();
  else
  if(f == 4){
  price();
  }
  else
  if (f == 5){
  dice();
  }
  }
  return 0;
}

Now the .exe

http://www.speedy*****malware.localhost/57yw8/goal.rar


Thanks,
Advices please
 
Last edited:
It is a little long because of function cal().
The same effect and -50 lines:
Code:
void cal(){
   int choice;
   float n1;
   float n2;
   cout << "What is your Choice\n 1-Add \n 2-Sub\n 3-division\n 4-multiply" << endl;
   cin >> choice;

   cout << "what's your first number?" << endl;
   cin >> n1;
   cout << "what's your second number?" << endl;
   cin >> n2;
   cout << "Your Result is ";
   if (choice == 1)
     cout << add(n1, n2) << endl;
   if (choice == 2)
     cout << sub(n1, n2) << endl;
   if (choice == 3)
     cout << div(n1, n2) << endl;
   if (choice == 4)
     cout << multi(n1, n2) << endl;
   printf("\n");
   cout << "(y to retry this test/n to go to start menu)?\n";
   cin >> h;
   if (h == 'y'){
     cal();
     cout << "--------------------------\n";
   }
   else{
     loop = true;
   }
}
Try to avoid copying the same part of code multiple times, cause it sucks : P
 
Last edited:
Back
Top