Question:
Please help in C++ runtime error for 10 points?
Ryan
2013-04-04 07:19:36 UTC
// runtime error is at bottem
// Template finds half of any value
#include
#include
#include
using namespace std;
template
double half(T)
{
double h = x * 1.0 / 2;
return(T);
}

void main()
{
int a = 47;
double b= 39.25;
cout<<"Half of "< cout<<"Half of "< getch();
}

Errors:
1>------ Build started: Project: hello boys, Configuration: Debug Win32 ------
1> Lab 10.1.cpp
1>Lab 10.1.cpp(20): warning C4996: 'getch': The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: _getch. See online help for details.
1> C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\conio.h(128) : see declaration of 'getch'
1>Lab 10.1.cpp(10): error C2065: 'x' : undeclared identifier
1> Lab 10.1.cpp(18) : see reference to function template instantiation 'double half(T)' being compiled
1> with
1> [
1> T=int
1> ]
1>Lab 10.1.cpp(11): error C2059: syntax error : ';'
1>Lab 10.1.cpp(10): error C2065: 'x' : undeclared identifier
1> Lab 10.1.cpp(19) : see reference to function template instantiation 'double half(T)' being compiled
1> with
1> [
1> T=double
1> ]
1>Lab 10.1.cpp(11): error C2059: syntax error : ';'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Three answers:
husoski
2013-04-04 08:00:43 UTC
Those are compile errors, not runtime errors. The build fails so you can't run...no executable file is produced on a build error.



The problem is in your template. You left out the variable name x in the header and are trying to return the template type T instead of of the computed h value.



template

double half(T x)

{

double h = x * 0.5;

return h;

}



The x*1.0/2 syntax you used groups as (x*1.0)/2, and multiplying by 1.0 does nothing, except have the effect of promoting an integer type to double. It's faster and simpler to just multiply by 0.5 instead. That will also get you a warning (as it should) if you ever use half() on a long double (officially part of the C++11 update, but unofficially has been around for a decade or so.)



Since h doesn't really do anything, you could just:



template

double half(T x) { return x*0.5; }



If your prof doesn't line one-liners, or if you don't, go back to 4 lines. But for simple templates like this, I think it's a nice, concise way to say "half(x)" means "(x*0.5)".



PS: About the deprecation message...ignore it. That's a C message that shouldn't be issued in C++. The getch() function defined by is NOT the POSIX getch() that they are warning about, and any standard library names in C++ are fair game now that the std:: namespace exists.



is nonportable, and a leftover from Microsoft C under MS-DOS. You don't need it for this purpose though, unless you need to run console programs by clicking on icons. Running without debug (Ctrl+F5) automatically pauses at end of run on a Win32 Console project, not matter how the program ended. (The getch() will only work if you end normally by returning from main, which won't happen on an uncaught exception, or some subroutine calling exit().)



If you don't get that behavior on Visual C++ 2010 or later, make sure you're creating a Win32 Console project and click on Application Setting BEFORE you click on Finish, and set "Empty Project" there to avoid the nonstandard boilerplate code from MS. Don't use the "Empty Project" template.



To fix up an "Empty Project" and make it a "Console Application":

1. Right click on the project and select "Properties".

2. Select "All Confiigurations" in the Configuration box.

3. Open the Linker item in the navigation tree and select System.

4. Select Console in the SubSystem drop-down box and click OK.
2016-08-11 04:56:13 UTC
Relying on your environment it'll both be (b) runtime error or (e) not one of the above (or without a doubt (c) compiler error if the compiler has any sense and prevents silly things early on). Int *p = 10; // assigns p as a pointer to int to deal with 10. Printf("%dn", *p); // dereferences tackle 10 as an integer. One among two things will occur in the course of the printf. Firstly 10 is quite often not a valid memory handle, or as a minimum no longer one a humble program is allowed to seem at, so you will get a safety fault, that may be a runtime error. Secondly 10 may be a valid memory deal with, however the changes of it containing 10, 5 or anyother given quantity are faraway (ordinarily exactly 2 to the vigor 32 on a 32-bit computer), as a consequence (e) none of the above.
Jacob
2013-04-04 07:38:06 UTC
first of all, your function doesn't do anything, try this instead:



double half(double a)

{

return a/2;

}



That's all you have to do, right now you are returning T, which is an empty value because you didn't change it in any way.

also, put an underscore ('_') in front of getch(), the way you are using it is deprecated.



Good Luck!


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...