Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Call to a Function more than once (http://www.programmingforums.org/showthread.php?t=14494)

Chronos Nov 17th, 2007 9:14 AM

Call to a Function more than once
 
I have a program to write as part of assignment for class, using C++ of course and using functions. Basically I have to write a Resistor Colour Code Calculator which will allow a user to input the colours and it would give them a value as well as a tolerance range. I am currently having trouble writing the part for the first two bands which are the significant figures. Form what I can see I need to call the Significant Figures function twice, and I have no idea how to do it.

int sigfig () //Significant Figures Function
{
string sband1,sband2,sband3,sband4;
int nband,nband2,nband3,nband4, count;

cout << "Please enter colour of band 1";
cin >> sband1;
//while (count !=1)
//{
//count = count +1;
if (sband1 == "black")
{
nband=0;
}


int main() // Main Function
{
string band1, band2, band3, band4, result, colour, func, band;

band1 = sigfig ();
cout << band1;

cout << "Please enter colour of band 2";
cin >> band2;

band2 = sigfig ();
cout << band2;

cout << "Please enter colour of band 2";
cin >> band2;
cout << "Please enter colour of band 3";
cin >> band3;
cout << "Please enter colour of band 4";
cin >> band4;

DaWei Nov 17th, 2007 9:46 AM

Re: Call to a Function more than once
 
Your question isn't clear. Functions are designed to be called repeatedly. Otherwise, you might as well use inline code and booger up your footprint.

Please, henceforth, put your code in code tags. Because of the ugligness and unformatted nature of the code, I won't even read it. Consequently, I cannot give you a meaningful comment, other than the above.

Chronos Nov 17th, 2007 10:49 AM

Re: Call to a Function more than once
 
All I did was use the wrong code tags, didn't realise that there were more than one of them. I have solved the problem without the use of functions but the teacher wants it done with functions.


What i meant is that I want to call the function twice, for both band 1 and band 2. And having it being able to read the colour and give me the number for both those bands.

Note the following code is not the full code but just parts of it.

:

  1. int sigfig () //Significant Figures Function
  2. {
  3. string sband1,sband2,sband3,sband4;
  4. int nband,nband2,nband3,nband4, count;
  5.  
  6. cout << "Please enter colour of band 1";
  7. cin >> sband1;
  8.  
  9. if (sband1 == "black")
  10. {
  11. nband=0;
  12. }
  13.  
  14. int main() // Main Function
  15. {
  16. string band1, band2, band3, band4, result, colour, func, band;
  17.  
  18. band1 = sigfig ();
  19. cout << band1;
  20.  
  21. cout << "Please enter colour of band 2";
  22. cin >> band2;
  23.  
  24. band2 = sigfig ();
  25. cout << band2;
  26.  
  27. cout << "Please enter colour of band 2";
  28. cin >> band2;
  29. cout << "Please enter colour of band 3";
  30. cin >> band3;
  31. cout << "Please enter colour of band 4";
  32. cin >> band4;


Wizard1988 Nov 17th, 2007 11:25 AM

Re: Call to a Function more than once
 
:

void myFunction()
{
  cout << "Hello there!\n";
}

int main()
{
 myFunction();
 myFunction();
 return 0;
}


myFunction() is called twice in this example.

Chronos Nov 17th, 2007 2:40 PM

Re: Call to a Function more than once
 
Okay wizard, now after that call to that function is made twice I need to store two different variables, using that same function.

But thanks for your help, that seems like with will help me out a lot. I'll stop by if I have any more problems.

Wizard1988 Nov 17th, 2007 4:27 PM

Re: Call to a Function more than once
 
Well in the example I gave you the function returns void which is nothing. You might want to return an integer, int.

:

int add(int a, int b)
{
  return (a+b);
}


This code would simply add a and b and return it.

I hope this helps a bit more

WaltP Nov 18th, 2007 2:23 AM

Re: Call to a Function more than once
 
Quote:

Originally Posted by Chronos (Post 136985)
Okay wizard, now after that call to that function is made twice I need to store two different variables, using that same function.

OK then :rolleyes:
:

int myFunction()
{
  cout << "Hello there!\n";
  return x;
}

int main()
{
  int a, b;
  a = myFunction();
  b = myFunction();
 return 0;
}


Wizard1988 Nov 18th, 2007 4:22 AM

Re: Call to a Function more than once
 
Quote:

Originally Posted by WaltP (Post 137016)
OK then :rolleyes:
:

int myFunction()
{
  cout << "Hello there!\n";
  return x;
}

int main()
{
  int a, b;
  a = myFunction();
  b = myFunction();
 return 0;
}


I am afraid that that will not work;) the variable x does not exist inside your version of myFunction();

WaltP Nov 18th, 2007 11:25 PM

Re: Call to a Function more than once
 
Sorry, next time I'll write the OP's code for them... :icon_confused:

Dameon Nov 18th, 2007 11:30 PM

Re: Call to a Function more than once
 
Quote:

Originally Posted by WaltP (Post 137067)
Sorry, next time I'll write the OP's code for them... :icon_confused:

No. You should, however, at least try to be correct.


All times are GMT -5. The time now is 3:29 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC