473,394 Members | 1,840 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Build this simple progam according to earlier standards

13
I am new to C++ and have learned that when I build a program and I want to learn how, it is best to build each program according to the same standard.

In my programs I want to have a function protocol before the main section and a function definition after the main section.

Now I would like to build a program that takes 2 integers and switches them, integer A becomes integer B and the other way around. Simple.

What I have now is this:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int ChangeInteger(int, int, int); // protocol
  5.  
  6. int main()
  7. {
  8. int IntegerA;
  9. int IntegerB;
  10. int IntegerTemp;
  11.  
  12. cout << "Type 1st integer: ";
  13. cin  >> IntegerA;
  14.  
  15. cout << "Type 2nd integer: ";
  16. cin  >> IntegerB;
  17.  
  18. IntegerTemp = 5;
  19.  
  20. cout << "The value of IntegerA is " << ChangeInteger(IntegerA, IntegerB, IntegerTemp) << endl;
  21.  
  22. system ("pause");
  23.  
  24. return 0;
  25. }
  26.  
  27. int ChangeInteger (int Integer_A, int Integer_B, int Integer_Temp) // definition
  28.  
  29. {
  30. int result = Integer_B; 
  31. return result;
  32. }
  33.  
  34. int ChangeInteger2(int, int, int); // protocol
  35.  
  36. {
  37. int IntegerA;
  38. int IntegerB;
  39. int IntegerTemp;
  40.  
  41. cout << "The value of IntegerB is " << ChangeInteger2(IntegerA, IntegerB, IntegerTemp) << endl;
  42.  
  43. int ChangeInteger2 (int Integer_A, int Integer_B, int Integer_Temp) // definition
  44.  
  45. int result2 = Integer_A;
  46. return result2;
  47. }
  48.  
This is not working, it is giving me the following error on the "{" sign that comes below the line with the "int ChangeInteger2 (int Integer_A, int Integer_B, int Integer_Temp)" function definition.

error C2447: '{' : missing function header (old-style formal list?)

By the way, is it good practice to have a protocol and a definition in each program?
Jul 22 '07 #1
5 1379
I am new to C++ and have learned that when I build a program and I want to learn how, it is best to build each program according to the same standard.

In my programs I want to have a function protocol before the main section and a function definition after the main section.

Now I would like to build a program that takes 2 integers and switches them, integer A becomes integer B and the other way around. Simple.

What I have now is this:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int ChangeInteger(int, int, int); // protocol
  5.  
  6. int main()
  7. {
  8. int IntegerA;
  9. int IntegerB;
  10. int IntegerTemp;
  11.  
  12. cout << "Type 1st integer: ";
  13. cin  >> IntegerA;
  14.  
  15. cout << "Type 2nd integer: ";
  16. cin  >> IntegerB;
  17.  
  18. IntegerTemp = 5;
  19.  
  20. cout << "The value of IntegerA is " << ChangeInteger(IntegerA, IntegerB, IntegerTemp) << endl;
  21.  
  22. system ("pause");
  23.  
  24. return 0;
  25. }
  26.  
  27. int ChangeInteger (int Integer_A, int Integer_B, int Integer_Temp) // definition
  28.  
  29. {
  30. int result = Integer_B; 
  31. return result;
  32. }
  33.  
  34. int ChangeInteger2(int, int, int); // protocol
  35.  
  36. {
  37. int IntegerA;
  38. int IntegerB;
  39. int IntegerTemp;
  40.  
  41. cout << "The value of IntegerB is " << ChangeInteger2(IntegerA, IntegerB, IntegerTemp) << endl;
  42.  
  43. int ChangeInteger2 (int Integer_A, int Integer_B, int Integer_Temp) // definition
  44.  
  45. int result2 = Integer_A;
  46. return result2;
  47. }
  48.  
This is not working, it is giving me the following error on the "{" sign that comes below the line with the "int ChangeInteger2 (int Integer_A, int Integer_B, int Integer_Temp)" function definition.

error C2447: '{' : missing function header (old-style formal list?)

By the way, is it good practice to have a protocol and a definition in each program?
I think you mean prototype, not protocol. Anyways, your problem is that your changeinterger2 function has a prototype instead of a heading. remove the semicolon from that line and put the parameter variables in it. And put the function prototype at the top of your code with the other one.

Also, in both functions you are assigning a variable to a new name and then returning that, this isn't necessary.

Instead of this:
Expand|Select|Wrap|Line Numbers
  1. int result = Integer_B; 
  2. return result;
you could just put this:
Expand|Select|Wrap|Line Numbers
  1. return Integer_B;
And a similar thing with your other function.
Jul 22 '07 #2
Break2
13
Thanks for that. I just did as you said (except for the 'result' part, I will do that later). My code now looks like:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int ChangeInteger(int, int); // prototype
  5. int ChangeInteger2(int, int); // prototype
  6.  
  7. int main()
  8. {
  9. int IntegerA;
  10. int IntegerB;
  11.  
  12. cout << "Type 1st integer: ";
  13. cin  >> IntegerA;
  14.  
  15. cout << "Type 2nd integer: ";
  16. cin  >> IntegerB;
  17.  
  18. cout << "The value of IntegerA is " << ChangeInteger(IntegerA, IntegerB) << endl;
  19.  
  20. system ("pause");
  21.  
  22. return 0;
  23. }
  24.  
  25. int ChangeInteger (int Integer_A, int Integer_B) // definitie
  26.  
  27. {
  28. int result = Integer_B; 
  29. return result;
  30. }
  31.  
  32. {
  33. int ChangeInteger2(Integer_A, int Integer_B)
  34.  
  35. int IntegerA;
  36. int IntegerB;
  37.  
  38. cout << "The value of IntegerB is " << ChangeInteger2(IntegerA, IntegerB) << endl;
  39.  
  40. int ChangeInteger2 (int Integer_A, int Integer_B) // definition
  41.  
  42. int result2 = Integer_A;
  43. return result2;
  44. }
  45.  
I am still getting a missing function header error on the line with the bracket above: int ChangeInteger2(Integer_A, int Integer_B, int Integer_Temp)
Jul 22 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
line 33 starts a function definition for

int ChangeInteger2(Integer_A, int Integer_B)

except you have no left brace.


line 40 which is inside (int ChangeInteger2(Integer_A, int Integer_B)

defines

int ChangeInteger2 (int Integer_A, int Integer_B) // definition

again.

It looks like you wanted to call ChangeInteger() inside ChangeInteger2() but didn't code th call correctly.
Jul 22 '07 #4
Break2
13
I still dont understand something, you say I do not have a left brace but on line 32 I have the left brace. Or I am just missing something?

I actually want to have the changeinteger part to be applicable to integer a changing into integer b and I have used the changeinteger2 part to change integer b into integer a.
Jul 22 '07 #5
weaknessforcats
9,208 Expert Mod 8TB
int ChangeInteger2(Integer_A, int Integer_B)
<<<<<<<<<<<
int IntegerA;
int IntegerB;

cout << "The value of IntegerB is " << ChangeInteger2(IntegerA, IntegerB) << endl;

int ChangeInteger2 (int Integer_A, int Integer_B) // definition

int result2 = Integer_A;
return result2;
}
I don't see a left brace here( <<<<<<).

Plus what is this line for:

int ChangeInteger2 (int Integer_A, int Integer_B) // definition

It looks like:

int ChangeInteger2(Integer_A, int Integer_B)

and the compiler will see it as trying to define a function within a function and what won't compile.
Jul 24 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: mwazir | last post by:
Hi all, Needed a clarification on a simple point. What is the real difference between Build and Rebuild option in the .NET IDE and when should either of the options be used? I tried searching...
0
by: Rich | last post by:
I am trying to obtain more debugging information for an issue I reported (ref. Bug #37185 <http://bugs.php.net/bug.php?id=37185&edit=2>). Unfortunately, I do not have access to the now-ancient...
176
by: nw | last post by:
Hi, I previously asked for suggestions on teaching testing in C++. Based on some of the replies I received I decided that best way to proceed would be to teach the students how they might write...
7
by: John Nagle | last post by:
Back in March, I posted this: That was for M2Crypto 0.17. It's still broken in M2Crypto 0.18. And there's no RPM or Windows binary. Nobody actually uses this stuff, do they?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.