How to make function repeat?

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 1290 Views - Last Post: 12 March 2012 - 02:57 AM Rate Topic: -----

#1 ASasquatch  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 11-March 12

How to make function repeat?

Posted 11 March 2012 - 10:16 AM

Hello , just started to learn C++ few days ago and today i wanted to try what i have learned , code works fine , but the problem is , i dont know , how to repeat the function on key press !

#include <iostream>
using namespace std;



int nUzdevums()
{
		cout << "Ieraksti cenu " << endl;
		float cena;
		cin >> cena;
		cout << "Ieraksti preces daudzumu " << endl;
		int daudzums;
		cin >> daudzums;
		cout << "Kopaa tu samaksasi " << cena*daudzums << " latus";
		cout << endl;
		cout << "..........................................";
		cout << endl;
		cout << endl;
		cout << "Vai velies atkartot operaciju? Y = Jaa , N = Nee";
		return 0;
}


int main()
	{
		nUzdevums();
		cin.clear();
		cin.ignore(255, '\n');
		cin.get();
}



What i am trying to do here is when user press Y , it does the nUzdevums(); again , but on N press , it exits the console :/

Is This A Good Question/Topic? 0
  • +

Replies To: How to make function repeat?

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5666
  • View blog
  • Posts: 22,509
  • Joined: 23-August 08

Re: How to make function repeat?

Posted 11 March 2012 - 10:25 AM

If you need to repeat something in a programming language, you generally use loops.
Was This Post Helpful? 0
  • +
  • -

#3 ASasquatch  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 11-March 12

Re: How to make function repeat?

Posted 11 March 2012 - 10:41 AM

View PostJackOfAllTrades, on 11 March 2012 - 10:25 AM, said:

If you need to repeat something in a programming language, you generally use loops.


I`ve been following learncpp.com tutorial , but im not so far there yet , so i dunno how to use and what is loops ! So is here a simple code that just do the trick?
Was This Post Helpful? 0
  • +
  • -

#4 Toadill  Icon User is offline

  • D.I.C Regular

Reputation: 40
  • View blog
  • Posts: 346
  • Joined: 08-January 12

Re: How to make function repeat?

Posted 11 March 2012 - 11:04 AM

Or you could just make the function recursive.

Honestly if you don't know what a loop is then you should be researching. They are not hard to learn.

I would suggest a
do

while

loop since you have to display the menu at least once.

Loops

Recursive Functions

This post has been edited by Toadill: 11 March 2012 - 11:18 AM

Was This Post Helpful? 0
  • +
  • -

#5 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 968
  • View blog
  • Posts: 3,379
  • Joined: 19-February 09

Re: How to make function repeat?

Posted 11 March 2012 - 11:26 AM

Hi, you also need to capture the key input :

  char atkartot;

  cout << "Vai velies atkartot operaciju? Y = Jaa , N = Nee";
  cin  >> atkartot;


Was This Post Helpful? 0
  • +
  • -

#6 ASasquatch  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 11-March 12

Re: How to make function repeat?

Posted 11 March 2012 - 12:39 PM

Really cant figure this out :/ I know im doing it wrong !

		char atkartot;
		cout << "Vai velies atkartot operaciju? Y = Jaa , N = Nee ";
		cin >> atkartot;
		if ( atkartot == getchar(Y) )
		{
			cout << nUzdevums();
		}
		return 0;


Was This Post Helpful? 0
  • +
  • -

#7 shurd  Icon User is offline

  • D.I.C Head

Reputation: 37
  • View blog
  • Posts: 158
  • Joined: 05-February 12

Re: How to make function repeat?

Posted 11 March 2012 - 01:03 PM

getchar() function takes a char out of your standard input method and puts it into a assigned variable, so thats not what you want
//this should be
atkartot == getchar(Y)
//that as you want to compare your variable to an answer
atkartot == 'Y'



Example of getchar();
#include <iostream>
int main()
{
   char c;
   cout<<"Enter a text('.' marks the end)\n";
   do
   {
      c=getchar();
      putchar(c);
   }while(c!='.');
   /*also an example of a loop, the do...while
   loop will do things you specify between than
   and test(like an if) after doing what you
   specified to see if it needs to end.
   In this case will do until c matches a dot.*/
   return 0;
}


This post has been edited by shurd: 11 March 2012 - 01:04 PM

Was This Post Helpful? 0
  • +
  • -

#8 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 390
  • View blog
  • Posts: 1,348
  • Joined: 31-December 10

Re: How to make function repeat?

Posted 11 March 2012 - 01:09 PM

View PostToadill, on 11 March 2012 - 01:04 PM, said:

I would suggest a
do

while

loop since you have to display the menu at least once.

I would suggest a while loop because do-while loops can be tricky to novices. For example, if you just want to ask the user if they want to run the program again, it would be:
/* set ans equal to 'y' so the loop runs at least once */
char ans = 'y';

/* keep looping if ans equals 'y' or 'Y' */
while(ans == 'y' || ans == 'Y')
{
     /* code you want repeated goes here */

     cout << "Try Again(y/n)> ";
     cin >> ans;
}


If the code you place inside the loop asks for user input, then the user types something in and hits <ENTER>. This will leave a newline('\n') character inside the istream's buffer so with the above code, ans would end up being set to '\n', which is not what we want. If this happens, just place a call to cin.ignore(), cin.get(), etc., so the newline gets discarded.

This post has been edited by vividexstance: 11 March 2012 - 01:12 PM

Was This Post Helpful? 2
  • +
  • -

#9 ASasquatch  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 11-March 12

Re: How to make function repeat?

Posted 11 March 2012 - 02:16 PM

@vividexstance

The Y worked fine ,but it made an infinite loop and messed up code somehow !

int main()
	{
		nUzdevums();

		char ans = 'y';
		while(ans == 'y' || ans == 'Y')
		{
			nUzdevums();

		}

		cin.clear();
		cin.ignore(255, '\n');
		cin.get();
}



I tought it would be much easier to make something like this :/
Was This Post Helpful? 0
  • +
  • -

#10 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 390
  • View blog
  • Posts: 1,348
  • Joined: 31-December 10

Re: How to make function repeat?

Posted 11 March 2012 - 02:20 PM

You need to ask the user inside the loop if they want to continue or not. Look at what I posted and what you posted, do you see the difference?

This post has been edited by vividexstance: 11 March 2012 - 02:21 PM

Was This Post Helpful? 0
  • +
  • -

#11 Bryston  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 125
  • Joined: 24-January 12

Re: How to make function repeat?

Posted 11 March 2012 - 02:20 PM

You are almost there, you just aren't allowing the user to enter y or Y to repeat, take another look at the example.
Was This Post Helpful? 0
  • +
  • -

#12 ASasquatch  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 11-March 12

Re: How to make function repeat?

Posted 11 March 2012 - 02:36 PM

Oh come on , im too newbie to figure this out , getting sick from my lack of knowledge , gonna learn harder tommorow :D
Was This Post Helpful? 0
  • +
  • -

#13 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 390
  • View blog
  • Posts: 1,348
  • Joined: 31-December 10

Re: How to make function repeat?

Posted 11 March 2012 - 02:41 PM

Just follow the instructions and everything should be ok, I really don't understand what the problem is. You really can't see the difference between what I posted and what you posted??? The difference is, like Bryston pointed out, is that you are trying to ask the user if they want to continue after the close of the while-loop. This will not work because the only way for the program to repeat is for the code you want repeated to be placed inside the while-loop.

Just think about it, we start off with ans equal to 'y'. The loop checks ans' value against 'y' or 'Y'. The first time through the loop condition, this is true. Then the code you want repeated is executed. Then finally the program should ask the user if they want to continue or not. If they say no('n'), then the program should quit, if they say yes('y') then the loop needs to start over.

Let us know what it is that you don't understand about this. If you don't ask the user if they want to continue inside of the loop, then you will have an infinite loop like you said. The reason is because the user never has a chance to enter 'n' to exit the program if you never ask them in the loop.

This post has been edited by vividexstance: 11 March 2012 - 02:42 PM

Was This Post Helpful? 0
  • +
  • -

#14 ASasquatch  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 11-March 12

Re: How to make function repeat?

Posted 11 March 2012 - 03:04 PM

I dont really understand the whole while stuff !

Still my code is broken and looks like from the beginning !

// functions.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


int _tmain(int argc, _TCHAR* argv[])
{
	return 0;
}

// sākas projekts

#include <iostream>
using namespace std;


int nUzdevums()
{
		cout << "Ieraksti cenu " << endl;
		float cena;
		cin >> cena;
		cout << "Ieraksti preces daudzumu " << endl;
		int daudzums;
		cin >> daudzums;
		cout << "Kopaa tu samaksasi " << cena*daudzums << " latus";
		cout << endl;
		cout << "..........................................";
		cout << endl;
		cout << endl;
		cout << "Vai velies atkartot operaciju? Y = Jaa , N = Nee ";
		return 0;
}

int main()
	{
		nUzdevums();

		if char ans = 'y';
		while(ans == 'y' || ans == 'Y')
		{
			nUzdevums();
		}

		cin.clear();
		cin.ignore(255, '\n');
		cin.get();
}







The question already is in nUzdevums(), but how wait for the answer in main i dont know , i just dont get it , i must see the whole thing done , than i can figure out what went wrong and why !
Was This Post Helpful? 0
  • +
  • -

#15 Bryston  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 125
  • Joined: 24-January 12

Re: How to make function repeat?

Posted 11 March 2012 - 03:11 PM

View PostASasquatch, on 11 March 2012 - 02:36 PM, said:

Oh come on , im too newbie to figure this out , getting sick from my lack of knowledge , gonna learn harder tommorow :D


Try it like this, write some comments first defining what you want to do, and where you want to do it, then write the code to fulfill the comments.

for example
char answer;
//set up a loop that will run as long as user enters Y or y for yes
while(answer == 'y' || answer == 'Y'){
    //do some stuff here
    some_function();
    //ask the user whether he wants to run again
    cout << "Run again? :>";
    //get users answer
    cin >> answer;

}




When I first started I found this approach helped a lot.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2