14 Replies - 3566 Views - Last Post: 03 November 2010 - 10:38 AM Rate Topic: -----

#1 Guest_Jack*


Reputation:

Addition loop using a while statement?

Posted 02 November 2010 - 09:04 AM

Alright. My problem is that I cant figure out how to create a loop that will allow me to add up an basically infinate ammount of numbers. I need to use functions for the addition and i think thats where im running into my problem. I have been messing around with putting the loop in the main body of code and even in the function but i cant figure it out. My task is to use while and switch statemtns to add up a bunch of numbers then divide that by a predetermined number. Below is the code i have so far minus any switches or loops. All it can do now is add up 3 numbers. Any suggestions?

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

double UserInput(string label)
{
	int rtVal=0;
	cout << label <<"=";
	cin>>rtVal;
	return rtVal;
}
double Add(double x, double y, double q)
{
	double z=0;
	z=x+y+q;
	return z;
}

double Divide(double s, double t)
{
	double z=0;
		z=s/t;
	return z;
}

void UserOutput(double output)
{
	cout <<"cost= " << output<<endl;
}


int _tmain(int argc, _TCHAR* argv[])
{
	double a=0;
	double b=0;
	double c=0;
	double d=0;
	double e=0;
	double f=0;


	a=UserInput("A");
	a=UserInput("B");
	a=UserInput("C");

	e=Add(a,b,c);

	f=Divide(e,1000);

	UserOutput(f);



	return 0;
}


MOD EDIT: When posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 03 November 2010 - 10:48 AM


Is This A Good Question/Topic? 0

Replies To: Addition loop using a while statement?

#2 Djabby  Icon User is offline

  • D.I.C Head

Reputation: 37
  • View blog
  • Posts: 131
  • Joined: 02-November 10

Re: Addition loop using a while statement?

Posted 02 November 2010 - 09:08 AM

View PostJack, on 02 November 2010 - 08:04 AM, said:

	a=UserInput("A");
	a=UserInput("B");
	a=UserInput("C");



You assign three times the value to a. It should be:

	a=UserInput("A");
	b=UserInput("B");
	c=UserInput("C");


Was This Post Helpful? 0
  • +
  • -

#3 Guest_Jack*


Reputation:

Re: Addition loop using a while statement?

Posted 02 November 2010 - 09:29 AM

alright. i changed that and it works, but where should i even start to add up an infinite ammount of numbers? I really have no idea where to begin. Do i need to change my function, or can i somehow accomplish it in the main body. Iv been scouring the net, but not much help with functions.
Was This Post Helpful? 0

#4 Djabby  Icon User is offline

  • D.I.C Head

Reputation: 37
  • View blog
  • Posts: 131
  • Joined: 02-November 10

Re: Addition loop using a while statement?

Posted 02 November 2010 - 09:33 AM

View PostJack, on 02 November 2010 - 08:29 AM, said:

alright. i changed that and it works, but where should i even start to add up an infinite ammount of numbers? I really have no idea where to begin. Do i need to change my function, or can i somehow accomplish it in the main body. Iv been scouring the net, but not much help with functions.


A function that gets infinite parameters is irrealistic. Better to try to do it in a loop:

#include <iostream>
using namespace std;
int main()
{
  int n;
  int sum;
  int num;
  cout << "Amount of numbers you want to add: ";
  cin >> n;
  while(n)
  {
    cin >> num;
    sum+=num;
    n--;
  }
  cout << "The sum is " << sum << endl;
}


Was This Post Helpful? 0
  • +
  • -

#5 Guest_Jack*


Reputation:

Re: Addition loop using a while statement?

Posted 02 November 2010 - 09:56 AM

Hey, thx. That is what i was trying to do actually. Is it possible to use a while loop in a function?
Was This Post Helpful? 0

#6 Djabby  Icon User is offline

  • D.I.C Head

Reputation: 37
  • View blog
  • Posts: 131
  • Joined: 02-November 10

Re: Addition loop using a while statement?

Posted 02 November 2010 - 09:59 AM

View PostJack, on 02 November 2010 - 08:56 AM, said:

Hey, thx. That is what i was trying to do actually. Is it possible to use a while loop in a function?


Sure it is. An infinite loop is mostly done like that:

while(true)
{
   // this loop will never be leaved
}



But though, you should implement a possibility to terminate the process.

This post has been edited by Djabby: 02 November 2010 - 10:00 AM

Was This Post Helpful? 0
  • +
  • -

#7 Guest_Jack*


Reputation:

Re: Addition loop using a while statement?

Posted 02 November 2010 - 10:25 AM

thx again. You helped out alot. All i have to do now is figure out where i can put a switch statment in. I was thinking of adding a switch to allow the user to enter a new set of numbers after the first sequence. When they are done they can enter q or w/e to close the program. if i did this would i have to create a new while or can i add a switch after the original while? Or i was thinking i would have to create a while loop and make the original loop a case. What do you think would be the smartest approach?
Was This Post Helpful? 0

#8 Djabby  Icon User is offline

  • D.I.C Head

Reputation: 37
  • View blog
  • Posts: 131
  • Joined: 02-November 10

Re: Addition loop using a while statement?

Posted 02 November 2010 - 10:28 AM

View PostJack, on 02 November 2010 - 09:25 AM, said:

thx again. You helped out alot. All i have to do now is figure out where i can put a switch statment in. I was thinking of adding a switch to allow the user to enter a new set of numbers after the first sequence. When they are done they can enter q or w/e to close the program. if i did this would i have to create a new while or can i add a switch after the original while? Or i was thinking i would have to create a while loop and make the original loop a case. What do you think would be the smartest approach?


Smartest? Hm, I think something like that:

int n=1;
while(n)
{
  cout << "How many numbers do you want to enter? ";
  cin >> n;
  int i = n;
  while(i)
  {
    // read input and calculate sum here
    i--;
  }
}



A user can terminate if he enters 0. And he can decide himself how many numbers he wants to enter.
Was This Post Helpful? 0
  • +
  • -

#9 Guest_Jack*


Reputation:

Re: Addition loop using a while statement?

Posted 02 November 2010 - 10:52 AM

alright. im all screwed up now... i tried incorporating the first loop into my add function and it doesnt work now. It worked in my main body, but my task is to use at least 4 functions. Why is it not working? Also, im curios what what the a--; is at the end of the loop?

#include <iostream>
#include <string>
using namespace std;

double UserInput(string label)
{
	int rtVal=0;
	cout << label <<"=";
	cin>>rtVal;
	return rtVal;
}
double Add()
{
	double a=0;
	double b=0;
	double c=0;

	while(true)
	{
	b+=c;
	a--;  
	}

	return b;
}

double Divide(double s, double t)
{
	double z=0;
		z=s/t;
	return z;
}

void UserOutput(double output)
{
	cout <<"cost= " << output<<endl;
}


int _tmain(int argc, _TCHAR* argv[])
{ 

   double a=0;  
   double b=0;
   double c=0;
   double f=0;
   double cont=0;

    a=UserInput("a");
    
    c=Add();
   
   	f=Divide(c,1000);

	UserOutput(f);
		

     
	return 0;
}


MOD EDIT: When posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 03 November 2010 - 10:48 AM

Was This Post Helpful? 0

#10 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Addition loop using a while statement?

Posted 02 November 2010 - 10:53 AM

I would suggest:

while(true)
{
  cout << "How many numbers do you want to enter? (-1 to exit)";
  cin >> n;

  if (n == -1)
     break;

  while(n > 0)
  {
    // read input and calculate sum here
    n--;
  }
}



This post has been edited by n8wxs: 02 November 2010 - 10:54 AM

Was This Post Helpful? 0
  • +
  • -

#11 Djabby  Icon User is offline

  • D.I.C Head

Reputation: 37
  • View blog
  • Posts: 131
  • Joined: 02-November 10

Re: Addition loop using a while statement?

Posted 02 November 2010 - 11:33 AM

View PostJack, on 02 November 2010 - 09:52 AM, said:

double Add()
{
	double a=0;
	double b=0;
	double c=0;

	while(true)
	{
	b+=c;
	a--;  
	}

	return b;
}



Ummm. That is again wrong. Here you are adding infinitely zeros together.
And btw it is redundant to write an own Add-Function. The +-operator
is doing excellent jobs! And we don't want you to create a while-loop
inside of the Add-Function. Rather there where you receive and process the input.

This post has been edited by Djabby: 02 November 2010 - 11:34 AM

Was This Post Helpful? 0
  • +
  • -

#12 Guest_Jack*


Reputation:

Re: Addition loop using a while statement?

Posted 02 November 2010 - 11:59 AM

You guys have been great help so far. i probably wouldnt have gotten anywhere trying to figure this out myself. I still am a little confused though. I took the loop out the add function like you said, but then I deviated a little. I need to have a switch statement somewhere in my code, so i tried to add one at the end to allow the user to add up another cost. I doubt im doing it right though cause it keeps stopping withought allowing the user to enter any new numbers. Whats wrong?
By the way, thanks for bearing with me. I also realize that i dont need the add function up top anymore, but im keeping it there simply because we need at least four functions. Even if i dont use it i can still show that i know how they work.

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

double UserInput(string label)
{
	int rtVal=0;
	cout << label <<"=";
	cin>>rtVal;
	return rtVal;
}

int Add(int x, int y, int q)
{
	int z=0;
	z=x+y+q;
	return z;
}


double Divide(double s, double t)
{
	double z=0;
		z=s/t;
	return z;
}

void UserOutput(double output)
{
	cout <<"cost= " << output<<endl;
}


int _tmain(int argc, _TCHAR* argv[])
{ 
	int n=0;  
	int sum=0;  
	int num=0; 
	int cont=0;
	double f=0;
	double x=0;

   cout << "Enter amount of numbers you want to add: ";  

   cin >> n;  

   
   while(n)  
   {
	cin >> num;  
     sum+=num;  
	 n--;  
   }

    f=Divide(sum,1000);

	UserOutput(f);

	cout << "\n";

	cout << "Enter 1 to continue with the cost of another link or 2 to quit" << endl;
	
	cin >> x;

	switch (cont)
	{
		case 1:
			while(n)  
		   {
			 cin >> num;  
			 sum+=num;  
			 n--;  
		   }
			f=Divide(sum,1000);

			UserOutput(f);
	break;

		case 2:
			cout << "operation ended" << endl;
			break;
	}

	return 0;
}

  

Was This Post Helpful? 0

#13 Djabby  Icon User is offline

  • D.I.C Head

Reputation: 37
  • View blog
  • Posts: 131
  • Joined: 02-November 10

Re: Addition loop using a while statement?

Posted 02 November 2010 - 12:22 PM

Look at Line 64-66. You use the variables cont and x. Is that really what you want?
And before you enter more switch-statements like this, think of the power of a loop.
A loop helps you to repeat something you want to do.
Now you have multiple occurences of this code:

   while(n)  
   {
	cin >> num;  
     sum+=num;  
	 n--;  
   }



Is it really necessary? Can't you make an outer while-loop like this:

 while(..)
 {
   while(n)  
   {
	cin >> num;  
     sum+=num;  
	 n--;  
   }
 }



That is the basic idea of our code snippets we've posted yet. I really suggest you to test what we've posted and check if it matches your needs. And then try to figure out why it works how it works.

This post has been edited by Djabby: 02 November 2010 - 12:22 PM

Was This Post Helpful? 0
  • +
  • -

#14 Guest_Jack*


Reputation:

Re: Addition loop using a while statement?

Posted 02 November 2010 - 12:56 PM

Well. thx for giving me the hint. i figured it out and it works alright now. Its probably not good programming, but it accomplished the requirements. I do agree that the loop idea was better than what i was doing, but i needed that switch statement in there. After you pointed out what the issue was i realized i was missing a bit and had things pointing(if you will) to something that didnt exist. thx again.
Was This Post Helpful? 0

#15 animecrazy  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 22-May 10

Re: Addition loop using a while statement?

Posted 03 November 2010 - 10:38 AM

You can also try recursive functions to achieve this goal....try it....but be sure not to go for infinite numbers, because then your program will crash....
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1