Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 17th, 2007, 8:14 AM   #1
Chronos
Newbie
 
Chronos's Avatar
 
Join Date: Nov 2007
Location: Barbados
Posts: 4
Rep Power: 0 Chronos is on a distinguished road
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;
Chronos is offline   Reply With Quote
Old Nov 17th, 2007, 8:46 AM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Nov 17th, 2007, 9:49 AM   #3
Chronos
Newbie
 
Chronos's Avatar
 
Join Date: Nov 2007
Location: Barbados
Posts: 4
Rep Power: 0 Chronos is on a distinguished road
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.

C++ Syntax (Toggle Plain Text)
  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;
Chronos is offline   Reply With Quote
Old Nov 17th, 2007, 10:25 AM   #4
Wizard1988
Professional Programmer
 
Wizard1988's Avatar
 
Join Date: Oct 2005
Location: Chitown
Posts: 417
Rep Power: 3 Wizard1988 is on a distinguished road
Send a message via AIM to Wizard1988
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.
__________________
JG-Webdesign
Wizard1988 is offline   Reply With Quote
Old Nov 17th, 2007, 1:40 PM   #5
Chronos
Newbie
 
Chronos's Avatar
 
Join Date: Nov 2007
Location: Barbados
Posts: 4
Rep Power: 0 Chronos is on a distinguished road
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.
Chronos is offline   Reply With Quote
Old Nov 17th, 2007, 3:27 PM   #6
Wizard1988
Professional Programmer
 
Wizard1988's Avatar
 
Join Date: Oct 2005
Location: Chitown
Posts: 417
Rep Power: 3 Wizard1988 is on a distinguished road
Send a message via AIM to Wizard1988
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
__________________
JG-Webdesign
Wizard1988 is offline   Reply With Quote
Old Nov 18th, 2007, 1:23 AM   #7
WaltP
Programmer
 
Join Date: Oct 2007
Posts: 39
Rep Power: 0 WaltP is on a distinguished road
Re: Call to a Function more than once

Quote:
Originally Posted by Chronos View Post
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
int myFunction()
{
  cout << "Hello there!\n";
  return x;
}

int main()
{
  int a, b;
  a = myFunction();
  b = myFunction();
 return 0;
}
WaltP is offline   Reply With Quote
Old Nov 18th, 2007, 3:22 AM   #8
Wizard1988
Professional Programmer
 
Wizard1988's Avatar
 
Join Date: Oct 2005
Location: Chitown
Posts: 417
Rep Power: 3 Wizard1988 is on a distinguished road
Send a message via AIM to Wizard1988
Re: Call to a Function more than once

Quote:
Originally Posted by WaltP View Post
OK then
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();
__________________
JG-Webdesign
Wizard1988 is offline   Reply With Quote
Old Nov 18th, 2007, 10:25 PM   #9
WaltP
Programmer
 
Join Date: Oct 2007
Posts: 39
Rep Power: 0 WaltP is on a distinguished road
Re: Call to a Function more than once

Sorry, next time I'll write the OP's code for them...
WaltP is offline   Reply With Quote
Old Nov 18th, 2007, 10:30 PM   #10
Dameon
Troll
 
Dameon's Avatar
 
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4 Dameon is on a distinguished road
Re: Call to a Function more than once

Quote:
Originally Posted by WaltP View Post
Sorry, next time I'll write the OP's code for them...
No. You should, however, at least try to be correct.
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270
Dameon is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Call the class main function quantalfred Java 6 Jul 23rd, 2006 1:38 PM
Call to undefined function mysql_connect() billpull PHP 11 Jul 13th, 2006 2:45 PM
How to call normal "write" function inside a class Edgar C++ 1 May 24th, 2006 6:35 PM
Function call from C++ program Klarre Assembly 4 Feb 15th, 2006 1:28 AM
aargh! include thing with function call Intimidat0r PHP 23 Aug 4th, 2005 6:34 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:14 AM.

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