473,396 Members | 1,827 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,396 software developers and data experts.

wont run

134 100+
this will compile with no errors, debug no errors- just doesnt really do any thing when it runs
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. void calculate(int a,int b,int& c); //you need to give the parameters after the name of your function
  6.  
  7. int main(){
  8. int sum;
  9. calculate(10,20,sum);
  10. cout<<sum;//the integers' value is now 30
  11. cin.get();
  12. return 0;
  13. }
  14.  
  15. void calculate(int a,int b,int& c) //the last parameter is a reference to an int, this will hold the sum of the 2 first integers
  16. {
  17. c=a+b;
  18. }
  19.  
May 8 '07 #1
16 1627
RedSon
5,000 Expert 4TB
this will compile with no errors, debug no errors- just doesnt really do any thing when it runs
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. void calculate(int a,int b,int& c); //you need to give the parameters after the name of your function
  6.  
  7. int main(){
  8. int sum;
  9. calculate(10,20,sum);
  10. cout<<sum;//the integers' value is now 30
  11. cin.get();
  12. return 0;
  13. }
  14.  
  15. void calculate(int a,int b,int& c) //the last parameter is a reference to an int, this will hold the sum of the 2 first integers
  16. {
  17. c=a+b;
  18. }
  19.  
Well I see a few problems right of the bat. But the question begs, why not create a int calculate (int a, int b) method? That seems much easier then passing some reference and fiddling with all those fancy * & operators.
May 8 '07 #2
Savage
1,764 Expert 1GB
this will compile with no errors, debug no errors- just doesnt really do any thing when it runs
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. void calculate(int a,int b,int& c); //you need to give the parameters after the name of your function
  6.  
  7. int main(){
  8. int sum;
  9. calculate(10,20,sum);
  10. cout<<sum;//the integers' value is now 30
  11. cin.get();
  12. return 0;
  13. }
  14.  
  15. void calculate(int a,int b,int& c) //the last parameter is a reference to an int, this will hold the sum of the 2 first integers
  16. {
  17. c=a+b;
  18. }
  19.  
It's becasue ur function is of void type.Change it to the int and make it return the sum.

Savage
May 8 '07 #3
lumpybanana247
134 100+
It's becasue ur function is of void type.Change it to the int and make it return the sum.

Savage
return the sum? well, i tried this and it still didnt do anything

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int calculate(int a,int b,int c); //you need to give the parameters after the name of your function
  6.  
  7. int main(){
  8. int sum;
  9. calculate(10,20,sum);
  10. cout<<sum;//the integers' value is now 30
  11. cin.get();
  12. return 0;
  13. }
  14.  
  15. int calculate(int a,int b,int c) //the last parameter is a reference to an int, this will hold the sum of the 2 first integers
  16. {
  17. c=a+b;
  18. return 0;
  19. }
  20.  
PS: i got this out of some tutorial and when you say "why dont you use the ... function" i have no idea what you mean

thanks
May 8 '07 #4
Savage
1,764 Expert 1GB
return the sum? well, i tried this and it still didnt do anything

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int calculate(int a,int b,int c); //you need to give the parameters after the name of your function
  6.  
  7. int main(){
  8. int sum;
  9. calculate(10,20,sum);
  10. cout<<sum;//the integers' value is now 30
  11. cin.get();
  12. return 0;
  13. }
  14.  
  15. int calculate(int a,int b,int c) //the last parameter is a reference to an int, this will hold the sum of the 2 first integers
  16. {
  17. c=a+b;
  18. return 0;
  19. }
  20.  
PS: i got this out of some tutorial and when you say "why dont you use the ... function" i have no idea what you mean
Try by returning c instead of 0.

Savage
May 8 '07 #5
lumpybanana247
134 100+
Try by returning c instead of 0.

Savage
well, this


Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int calculate(int a,int b,int c); //you need to give the parameters after the name of your function
  6.  
  7. int main(){
  8. int sum;
  9. calculate(10,20,sum);
  10. cout<<sum;//the integers' value is now 30
  11. cin.get();
  12. return 0;
  13. }
  14.  
  15. int calculate(int a,int b,int c) //the last parameter is a reference to an int, this will hold the sum of the 2 first integers
  16. {
  17. c=a+b;
  18. return c;
  19. }
  20.  
  21.  
still doesnt work, but i dont know if thats right

remember, there is no error message and the exe file is made

thanks,
Nate
May 8 '07 #6
Savage
1,764 Expert 1GB
well, this


Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int calculate(int a,int b,int c); //you need to give the parameters after the name of your function
  6.  
  7. int main(){
  8. int sum;
  9. calculate(10,20,sum);
  10. cout<<sum;//the integers' value is now 30
  11. cin.get();
  12. return 0;
  13. }
  14.  
  15. int calculate(int a,int b,int c) //the last parameter is a reference to an int, this will hold the sum of the 2 first integers
  16. {
  17. c=a+b;
  18. return c;
  19. }
  20.  
  21.  
still doesnt work, but i dont know if thats right

remember, there is no error message and the exe file is made

thanks,
Nate
Ur function those need to be changed.

First u don't need sum in call to the function and secound when calling function u need to asign value that function returns the value to a variable like: sum=calculate(a,b)

Savage
May 8 '07 #7
lumpybanana247
134 100+
Ur function those need to be changed.

First u don't need sum in call to the function and secound when calling function u need to asign value that function returns the value to a variable like: sum=calculate(a,b)

Savage

i dont know exactly what you mean.

could you edit this for me?


#include <iostream>

using namespace std;

int calculate(int a,int b,int c); //you need to give the parameters after the name of your function

int main(){
int sum;
calculate(10,20,sum);
cout<<sum;//the integers' value is now 30
cin.get();
return 0;
}

int calculate(int a,int b,int c) //the last parameter is a reference to an int, this will hold the sum of the 2 first integers
{
c=a+b;
return 0;
}


thanks (if u wanna do it)

nate
May 8 '07 #8
ilikepython
844 Expert 512MB
i dont know exactly what you mean.

could you edit this for me?


#include <iostream>

using namespace std;

int calculate(int a,int b,int c); //you need to give the parameters after the name of your function

int main(){
int sum;
calculate(10,20,sum);
cout<<sum;//the integers' value is now 30
cin.get();
return 0;
}

int calculate(int a,int b,int c) //the last parameter is a reference to an int, this will hold the sum of the 2 first integers
{
c=a+b;
return 0;
}


thanks (if u wanna do it)

nate
What he means is that you don't need to bring in a variable to the function to store the result in. You can create the variable in the function itself and return that value.
Expand|Select|Wrap|Line Numbers
  1. int calculate(int a, int b);
  2.  
  3. int main(){
  4.     int sum = calculate(10, 20);
  5.     cout << sum;
  6.     cin.get()
  7.     return 0;
  8. }
  9.  
  10. int calculate(int a, int b){
  11.     int sum = a + b;     //initialize value
  12.     return sum;           //bring value back to function caller
  13. }
  14.  
May 8 '07 #9
RedSon
5,000 Expert 4TB
i dont know exactly what you mean.

could you edit this for me?


#include <iostream>

using namespace std;

int calculate(int a,int b,int c); //you need to give the parameters after the name of your function

int main(){
int sum;
calculate(10,20,sum);
cout<<sum;//the integers' value is now 30
cin.get();
return 0;
}

int calculate(int a,int b,int c) //the last parameter is a reference to an int, this will hold the sum of the 2 first integers
{
c=a+b;
return 0;
}


thanks (if u wanna do it)

nate
Nate, its obvious you have no idea what you are doing or even know the basic syntax of C/C++. You are just copying code from some tutorial and pasting it into your window and hoping that it will do what you want it to. Take some time to read through the basic tutorials on this site and on others like cplusplus.com. This question is very very basic and it will help you alot to go through all of the tutorials. They will be able to explain it a bunch better then we will by this piece mail approach.
May 8 '07 #10
Ganon11
3,652 Expert 2GB
As to the original problem...I think perhaps the definition of the function does not match the declaration (there is an & in the prototype, but not in the definition). But the main problem is probably caused because you are not outputting an endl. Some compilers will not print anything from cout unless an endl is printed. Try adding that to the output statement and see if it works.
May 8 '07 #11
Ganon11
3,652 Expert 2GB
The original code compiles and runs perfectly fine on Bloodshed Dev C++ 4.9.9.2. It displays the result 30, waits for me to enter a character, and exits.
May 8 '07 #12
lumpybanana247
134 100+
alright red sun or w/e ur name is, my problem isnt that i dont know the language (of course i dont know it all) but that the program does not launch. you didnt help at all. i have looked at many tutorials and this one didnt work. Anybody could go around on forums saying "you have no clue what ur talking about, look at tutorials". Why waste ur time?

now to the rest who havnt wasted ur time,
i used the code
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int calculate(int a, int b);
  6.  
  7. int main(){
  8.     int sum = calculate(10, 20);
  9.     cout <<sum;
  10.     cin.get();
  11.     return 0;
  12. }
  13.  
  14. int calculate(int a, int b){
  15.     int sum = a + b;     //initialize value
  16.     return sum;           //bring value back to function caller
  17. }
  18.  
  19.  
and it compiles with no errors but nothing happens when i run it.


thanks to some of you.
May 8 '07 #13
lumpybanana247
134 100+
GANON: i have bloodshed dev cpp 5 somthing, the code doesnt work. any ideas?
May 8 '07 #14
lumpybanana247
134 100+
gannon- again, thanks for that comment, i think i messed up my compiler when i reinstalled it. basic programs dont do anything.
thanks to you and others who tried to help.

btw RedSOn, since my compiler doesnt work, i guess ur right. that means that i dont know c++. that only makes since.

(haha)

--Nate
May 8 '07 #15
RedSon
5,000 Expert 4TB
Nate, here is my response to your rudeness. Then I am going to drop this issue.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. void calculate(int a,int b,int& c); //you need to give the parameters after the name of your function
  6.  
  7. int main(){
  8. int sum;
  9. calculate(10,20,sum);
  10. cout<<sum;//the integers' value is now 30
  11. cin.get();
  12. return 0;
  13. }
  14.  
  15. void calculate(int a,int b,int& c) //the last parameter is a reference to an int, this will hold the sum of the 2 first integers
  16. {
  17. c=a+b;
  18. }
  19.  
First you do not initialize sum. You cannot rely on your compiler or your system to zeroize your memory for you. Creating variables and not initializing them is a poor programming practice and a common mistake made by beginners. Second when you calculate you pass by reference which is major overkill for a simple sum method like this. It would be much easier to pass in the two variables and sum them together and return the final answer. Passing by reference or passing the address of a variable opens you up to several threading issues and memory management issues that are very subtle and difficult to debug. For a simple program like this you wouldn't need to worry about it but any thing more complex if you are not careful your data could get stepped on. Also like was stated some systems require a newline to be output before it will display anything in the console.

PS: i got this out of some tutorial and when you say "why dont you use the ... function" i have no idea what you mean
Here you state that you did read a tutorial but decided to copy and paste the code anyway. This leads me to believe that you would rather copy and paste code then actually understand the concept and implement it. Also saying that you don't understand what Savage was talking about when he said

...ur function is of void type.Change it to the int and make it return the sum.
Tells me that you don't know what a function is or how to declare one and return a value. This is something that is basic syntax and there are several tutorials that will explain the basic syntax so you have a better idea of what you are doing.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int calculate(int a,int b,int c); //you need to give the parameters after the name of your function
  6.  
  7. int main(){
  8. int sum;
  9. calculate(10,20,sum);
  10. cout<<sum;//the integers' value is now 30
  11. cin.get();
  12. return 0;
  13. }
  14.  
  15. int calculate(int a,int b,int c) //the last parameter is a reference to an int, this will hold the sum of the 2 first integers
  16. {
  17. c=a+b;
  18. return 0;
  19. }
This program that you rewrote puts the nail in the coffin, this program not only shows that you do not know the language (which is fine, you are not expected to understand all the language although we encourage at least a basic understanding before asking a question) it also shows that you do not have any programming expierence in any language. Savage's advice was good but because you don't understand what you are doing (and just copying and pasting code) you change void return from calculate to int then return 0. Then changed calculate's parameter list to remove the pass by reference.

i dont know exactly what you mean.

could you edit this for me?
Finally with Savage's patient help you give up and copy your code to the forum and ask someone to do it for you. That is when I knew that if you did read some tutorials you did not read them all the way through. And you need to go back and read all of them again and learn the basics. If it wasn't for ilikepython coming to your rescue and giving you the code I'm sure you would still be stuck with this program.

Anybody could go around on forums saying "you have no clue what ur talking about, look at tutorials". Why waste ur time?
This question is easy, because we want to help people that have good questions that have not been asked a hundred times already and who show a minimum of understanding basic programming concepts. We do not want to repeat the same information over and over again. The prerequisite information that you need to know before asking questions exists at the top of this forum and on well known sites like cplusplus.com. We don't expect everyone to know everything about a language and then ask questions but we do expect you to know the basics like how to create a main method and how to create and call simple functions that return a value. You don't even know how to do that and you are starting by passing by reference? You need to learn to crawl before you can run.

...my compiler doesnt work
Actually your compiler probably works fine, it creates an executable that you can run and it doesn't fail or complain about anything. It probably has something to do with not having the endl in your cout line. You could easily see if your program is running properly by attaching a debugger and stepping through your code.

In the future I suggest you be more polite and take everyone's advice at face value. Take some time to read all the tutorials on this site and on cplusplus.com actually learn what they are talking about instead of copying and pasting code. And don't expect others to come swooping in to your rescue by editing your code for you.
May 9 '07 #16
lumpybanana247
134 100+
allright

thanks for the debugger info actually. but that original code did work when i reinstalled my complier

pe@ce brutha
May 9 '07 #17

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

Similar topics

4
by: Steve Westwood | last post by:
I have a WebForm button. Depending on calculation I wont to send a popup message to the web page. I am not sure of the approach. From the server side code I don't seem to be able to access the...
5
by: manokumar | last post by:
hiye, i notice that some if not all of my folders in winxp pro. are set as read only and its giving me some problem with development. so as the natural thing, i unchecked the read only option and...
2
by: Lasse Edsvik | last post by:
Hello Im trying to run a simple asp.net page and it wont execute......... i run aspnet_regiis -i and it runs successfully..... but my pages wont execute.... what's wrong?? /Lasse
7
by: dhnriverside | last post by:
Hi guys I've got a web application on a Win2k IIS5 server. I've been coding it using anonymous access, and have just come to test the AD stuff - trouble is - all i get is a 402.1 error ("not...
4
by: dhnriverside | last post by:
HI guys I've just written my first independent namespace for my library (yay me!). However, on trying to add it to my website project, it causes an error when I look at the website. It compiles...
5
by: Mr Newbie | last post by:
Debug.Assert( False, "Why wont I display ??") I am trying to use this in my code but it wont display. The app is running on my local machine and the above code under a button click event. What...
4
by: Harry Hudini | last post by:
I'm trying to install VS .Net, particularly the VB elements, but no matter what i select, i get an error at the end of the process simply saying that an error occurred. The installlog.txt file...
1
by: eewwttww | last post by:
how to show the "txt code" that the WebBrowser wont to save when he wont to save html+pictures with the "dialogbox" - before saving. this is the code to open the save "dialogbox" ...
1
by: Diz | last post by:
Hi, Can anybody tell me why my php script wont run properly unless i put it in a page all on its own? Is it antisocial? Does it want to be alone? I have put it in a page with background and links to...
3
by: =?Utf-8?B?SlA=?= | last post by:
<asp:GridView ID="gridResults" runat="server" AutoGenerateColumns="False" Width="98%" PageSize="25" AllowPaging="True" OnSorting="gridResults_Sorting" OnPageIndexChanging...
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: 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
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.