You are viewing our Forum Archives. To view or take place in current topics click here.
Help With A C++ Project [TCLite]
Posted:

Help With A C++ Project [TCLite]Posted:

Taylor
  • Winter 2017
Status: Offline
Joined: Apr 30, 201410Year Member
Posts: 5,940
Reputation Power: 15078
Status: Offline
Joined: Apr 30, 201410Year Member
Posts: 5,940
Reputation Power: 15078
As the title says, I need help with a project. Im pretty new to programming so it shouldnt be a problem for any of you that have been doing it for a while now.

Its for a project at school in my C++ class, here is the project information;

Create a program that will keep track of the smallest and the largest number that a user enters, then average all of the numbers together, including the smallest and largest.

Now, I have all of the code written, theres only one problem. In my program after the user enters a number, I ask the user if he/she wants to continue, Press 1 to Continue or Press 2 to Quit. When the user enters 2 after the first number, it quits and shows the correct information. However, when the user enters 1 to input another number is just crashes the program, forcing me to restart it.

Ive been looking over it for about 2 days and Ive tried fixing, but nothing I do works. I just dont know how to fix it, and would greatly appreciate some help.

Youre not doing the project for me, the project was due today and I had it graded on how complete it was and how it ran. But, I still want to know what is wrong for future reference. So, if someone could take a look at it, and let me know what to do to fix it, that would be awesome.

Here is the code;

//Taylor Lynch
//Mr Rine
//Compare
//Keep track of smallest and largest numbers entered by user, and average all
//Period 5
//December 4th, 2015

#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
#include <stdio.h>
#include <dos.h>

main()
{

   clrscr();
   textcolor(LIGHTCYAN);
   textbackground(BLACK);
   clrscr();
   
   int ans;
   float avg, num, sum, i, large, small, num2, avg2, ans2;
   
      cout<<\n;
      cout<<\n;
      cout<<Please enter your first number:   ;
      cin>>num2;
      large=num2;
      small=num2;
      i=1;
      sum=num2;

      cout<<\n\n;
      cout<<Would you like to enter another number and continue?\nPress 1 for Yes. Press 2 for No.   :;
      cin>>ans2;
      while(ans2!=2);
      {
      textcolor(LIGHTGREEN);
      textbackground(BLACK);
      avg2=sum/i;
      cprintf(\n\rThe average of the entered numbers is %.2f, avg2);
      cprintf(\n\rThe smallest number entered was %.2f, small);
      cprintf(\n\rThe largest number entered was %.2f, large);
      getch();
   return 0;
   }
do
{
      cout<<\n;
      cout<<\n;
      cout<<Enter the desired number:   ;
      cin>>num;
      if(num<small)

      {

      small=num;

      }

      if(num>large)

      {

      large=num;

      }

      i++;
      sum=num+sum;
      textcolor(LIGHTRED);
      textbackground(BLACK);
      cout<<\n\nWould you like to enter another number to continue?\nPress 1 for Yes. Press 2 for No.   :;
      cin>>ans;

      }

      while(ans!=2);
      
      {

      textcolor(LIGHTGREEN);
      textbackground(BLACK);
      avg=sum/i;
      cprintf(\n\rThe average of the entered numbers is %.2f, avg);
      cprintf(\n\rThe smallest number entered was %.2f, small);
      cprintf(\n\rThe largest number entered was %.2f, large);
   getch();
   }
return 0;
}
#2. Posted:
Taylor
  • Supporter
Status: Offline
Joined: Apr 30, 201410Year Member
Posts: 5,940
Reputation Power: 15078
Status: Offline
Joined: Apr 30, 201410Year Member
Posts: 5,940
Reputation Power: 15078
Bumping this, I'm still looking to resolve this issue.
#3. Posted:
speed
  • Summer 2023
Status: Offline
Joined: Jun 11, 200914Year Member
Posts: 9,897
Reputation Power: 3160
Motto: "I'l no I grew up to fast speed I no u will be little famous" - Famous_Energy
Motto: "I'l no I grew up to fast speed I no u will be little famous" - Famous_Energy
Status: Offline
Joined: Jun 11, 200914Year Member
Posts: 9,897
Reputation Power: 3160
Motto: "I'l no I grew up to fast speed I no u will be little famous" - Famous_Energy

while(ans2!=2);
   {
   textcolor(LIGHTGREEN);
   textbackground(BLACK);
   avg2=sum/i;
   cprintf(\n\rThe average of the entered numbers is %.2f, avg2);
   cprintf(\n\rThe smallest number entered was %.2f, small);
   cprintf(\n\rThe largest number entered was %.2f, large);
   getch();
  return 0;
  }


Right here you're saying that if the input is not 2, print out that stuff and terminate the program.

Also, your while loops are all sorts of **** up in general. I suggest you do a little reading on while loops and revise your code.
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.