Try something like this
#include <iostream>
class test {
public:
int* num;
};
int main() {
test *d = new test;
d->num = new int;
*(d->num) = 3;
std::cout << *(d->num);
}
Try something like this
#include <iostream>
class test {
public:
int* num;
};
int main() {
test *d = new test;
d->num = new int;
*(d->num) = 3;
std::cout << *(d->num);
}
string getPrintStr(int colorEnum) { switch (colorEnum) { case red: return "red"; case green: return "green"; case blue: return "blue"; } return "unknown color"; }
Your function should really be.
string getPrintStr(const colour & colorEnum)
{
switch (colorEnum)
{
case red:
return "red";
case green:
return "green";
case blue:
return "blue";
}
return "unknown color";
}
That's a new one 'enum operator'. What's that?
Do you mean something like below?
#include <iostream>
enum colour {red, green, blue};
std::ostream& operator <<(std::ostream & out, const colour & c)
{
switch (c)
{
case red:
return out << "red";
case green:
return out << "green";
case blue:
return out << "blue";
}
return out;
}
int main()
{
colour favourite;
favourite = green;
std::cout << favourite << std::endl;
return 0;
}
It works fine for regular formatted files such as WORD-documents and text files, but with movies it stops after about 705 bytes, although the file is 42MB. I think it's because there is a EOF character in the middle of the file which breaks up the while loop. Does anyone know how to solve this?
~G
EOF is not stored with the file but is return by the operating system when its encountered.
http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1048865140&id=1043284351
I'm having problems figuring out what you mean.
OMg omg! I found the solution... I should use g++ instead of gcc.. hehe Thanks anyway :)
Yeah that would do it. Did it myself from time to time.
Try changing this line
scanf("%d %d %d",a,b,c);
to
scanf("%d %d %d",&a,&b,&c);
A few more pointers..
main should return an integer
int main()
{
...
return 0;
}
and you should include the library math.h for the math function(s).
#include <stdio.h>
#include <math.h>
Line 27 should be:
c.str[m]=' ';
Line 29 should be:
for(int len=m+1;(*b.str)!='\0';len++)
This is how I would solve it:
class string
{
public:
char& operator[](int offset);
const char& operator[](int offset) const;
friend string operator+(const string & lhs, const string & rhs);
};
const char& string::operator[](int offset) const
{
if (offset < itssize)
return thestring[offset];
throw xboundary(offset);
return thestring[0];
}
char& string::operator[](int offset)
{
if (offset < itssize)
return thestring[offset];
throw xboundary(offset);
return thestring[0];
}
string operator+(const string & lhs, const string & rhs)
{
string temp(lhs.itssize + rhs.itssize);//itssize is size of strings
int i = 0;
for (i = 0; i < lhs.itssize; ++i)
temp.thestring[i] = lhs.thestring[i];
for (int j = 0; j < rhs.itssize; ++i, ++j)
temp.thestring[i] = rhs.thestring[j];
temp.thestring[temp.itssize] = '\0';
return temp;
}
If your still having problems try looking at the problem like this:
printf("%s",p + ('E' - 'A'));
Try looking at it like this:
printf("%s",p + (p[3] - p[1]));
or like this:
printf("%s",&p[4]);
Your constructor assume an integral type with its initializer
Array(unsigned arraySize):
data(0), size(arraySize)//should be data(). size(arraySize)
Oops, too early. I didn't see that data was a pointer. My bad, please disregard.
This member function, why you returning T()? Shouldn't it be NULL.
T getValue(unsigned index) const
{
if(index < size)
return data[index];
else
return T();
}
I would use a external data representation library(XDR).
Probably the easiest way is to include a boolean field with your objects 'previous state' and set and retrieve previous states from it.
I think it has to do with line 28 where your returning a DateTime..Try returning void.
char *value = people[i].name;
is potentially dangerous because you did not initialize / declare a variable for the name member from indices 2 to 4. Due to this, you might get garbage results, or the program could crash.
Not really, the structure was created in the global space so it will be bitwise zeroed..So you'll have zeros and not garbage.
I just ran your program and got results..What are you compiling/running this on?
It doesn't work because the member function 'test' has no idea what pAVec is. pAVec is an instance of the object Member created in main. If you require a copy or reference of pAVec in test then pass it via a function parameter.
Uninitialized arrays are bitwise zeroed..The values for a uninitialized int array are zero.
You can always use try-catch blocks to catch the exception. But in this cases of yours it seems not to need one.
If your method has a return type, you have to return this type in any place in the Sum method. Otherwise the compiler will throw an error.
You can do it:private void YourMethod() { int a = -2; int b = 3; int c = Sum(a, b); if (c > 0) { //Ok!! } else { //show a message if needed //that some of values are negative } } public int Sum(int a, int b) { if (a < 0 || b < 0) { return 0; } else { return a + b; } }
Just a quick question. What happens to your logic when both numbers(a and b) are zero?
Here's what I was suggesting when I mentioned using unsigned integers for parameters
using System;
namespace testit
{
class MainClass
{
public static void Main (string[] args)
{
UInt64 x = 0, y = 0;
bool loop = true;
while (loop)
{
try
{
Console.Write("Enter value a->");
x = UInt64.Parse(Console.ReadLine());
Console.Write("Enter value b->");
y = UInt64.Parse(Console.ReadLine());
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
continue;
}
loop = false;
}
Console.WriteLine(sum_them(x, y));
}
public static UInt64 sum_them(UInt64 a, UInt64 b)
{
return a + b;
}
}
}
Why won't you make the parameters unsigned integers?
Maybe if you look at this code you'll see what the this pointer is...
#include <iostream>
class myint
{
public:
myint(int val):itsvalue(val) {}
int getitsvalue() const { return itsvalue; }
void* get_this() const { return (void*)this; }
private:
int itsvalue;
};
int main(int argc, char**argv)
{
myint me(1234);
std::cout << me.get_this() << std::endl;//find this pointer value
std::cout << &me << std::endl;//find the address of me
std::cout << me.getitsvalue() << std::endl;
*(int*)&me = 9999;
std::cout << me.getitsvalue() << std::endl;
return 0;
}
I would look into the modulus operator using it something like below.
using System;
namespace testit
{
class MainClass
{
public static void Main (string[] args)
{
UInt64 x = 0;
Console.Write("Enter a four digit number->");
x = UInt64.Parse(Console.ReadLine());
Console.WriteLine(reverse_it(x));
}
public static UInt64 reverse_it(UInt64 num)
{
UInt64 reverse = 0;
byte [] ans = new byte[]{0,0,0,0};
ans[3] = (byte)(num % 10);
num = (num -= num % 10) / 10;
ans[2] = (byte)(num % 10);
num = (num -= num % 10) / 10;
ans[1] = (byte)(num % 10);
num = (num -= num % 10) / 10;
ans[0] = (byte)(num % 10);
num = (num -= num % 10) / 10;
for (UInt64 i = 3; i > 0; --i)
{
reverse += ans[i];
reverse *= 10;
}
reverse += ans[0];
return reverse;
}
}
}
On line 57...Try initializing your double variables to 0.0
Your function prototype is incorrect..You have
int giveMemoryBack (CDROM int *pCDROM1, int *pCDROM2, int *pCDROM3)
and it should be
int giveMemoryBack (CDROM *pCD1, CDROM *pCD2, CDROM *pCD3)
{
delete pCD1;
delete pCD2;
delete pCD3;
return 0;
}
and calling your function should be
// Call giveMemoryBack function
giveMemoryBack(pCDROM1, pCDROM2, pCDROM3);
Look at your inner condition
while (payrollAmount >= 0)
To exit this loop you must set payrollAmount < 0. What happens the next time around? payrollAmount is still set to < 0. You have to set payrollAmount back to zero when you exit the inner loop.
Shouldn't you do your totalPayroll += payrollAmount in the inner loop? As it is, your inner loop just prompts for and accepts a value for payrollAmount.
I tried something in your code and it seems to work. I essentially knocked the x value back by one if an exception is thrown...
sing System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Exceptional
{
class WorkerTest
{
static void Main(string[] args)
{
Worker[] workerArray = new Worker[2];
int workerID;
double hrSal;
for (int x = 0; x < workerArray.Length; x++)
{
try
{
loadId(x, out workerID);
loadSal(x, out hrSal);
workerArray[x] = new Worker(workerID, hrSal);//only reach here if no exceptions thrown
}
catch (Exception e)
{
Console.WriteLine(e.Message);
--x;//knock counter back by one and start again.
}
}
Console.WriteLine();
for (int i = 0; i < workerArray.Length; i++)
{
Console.WriteLine("Worker # {0} \nHourly salary {1}\n--------\n ", workerArray[i].workerID, workerArray[i].hrSal.ToString("C"));
}
}
private static void loadSal(int position, out double val)
{
Console.Write("Input an hourly Salary for the worker[{0}]: ", position);
val = Convert.ToDouble(Console.ReadLine());
if (val < 5.0 || val > 55.0)
{
throw new ArgumentException("Unexpected Salary Range.");
}
}
private static void loadId(int position, out int val)
{
Console.Write("Input a work identification number for worker[{0}]: ", position);
val = Convert.ToInt32(Console.ReadLine());
}
}
class Worker
{
public int workerID;
public double hrSal;
public Worker(int WorkerID, double HrSal)
{
workerID = WorkerID;
hrSal = HrSal;
}
}
}
I thought CDROM was an object not integer? You should use
int deleteCD(CDROM *one, CDROM *two) { delete one; delete two; return 0;
Ooops missed that.
Your giveMemoryBack function should be
int giveMemoryBack (int *pCDROM, int *pCDROM2, int *pCDROM3)
{
delete pCDROM1
delete pCDROM2
delete pCDROM3
return 0;
}
Note: Remember to set the original pointers back to NULL.
Found something else, your function call
// Call giveMemoryBack function
int giveMemoryBack(int *pCDROM1, int *pCDROM2, int *pCDROM3);
is incorrect. It should be.
// Call giveMemoryBack function
giveMemoryBack(pCDROM1, pCDROM2, pCDROM3);
Ok, that makes sense! I didn't think of implicit destructors cleaning up my object - again, still learning this stuff!
Thanks for the clarification. Now, I just need to figure out how to reassign the values instead of cleaning them up when the exception is thrown!
Usually you handle range and out of range situations without exceptions. You should be able to check for input validity before the constructor is called.
Thanks for your responses! The explanation that Momerath gave me an idea of why I'm getting the NullReferenceException - now I just need to figure out how to rewrite it so that my default values in Worker are used when the exception is called, rather than just setting those values to null. I'll post with an update as its available.
Gerard - I don't know too much about destructors other than using them for cleanup. Could they still be applicable if I want to reassign variables to a default value?
Just a side note* - Please forgive my ignorance as I'm still relatively new to the C# programming language. Again, thank you for your help! I'll get cracking at this again and update you all on the solution (HOPEFULLY without more questions, though additional aid is always appreciated! :-))
I think one of your problems stem from the constructor throwing an exception when one of your parameters is one of range. When the constructor throws an exception the destructor is called cleaning up the object.i.e. The object doesn't exist.
Here's a little code snippet that may help you understand what's happening..Code from here - http://stackoverflow.com/questions/188693/is-the-destructor-called-if-the-constructor-throws-an-exception
using System;
class Test
{
Test()
{
throw new Exception();
}
~Test()
{
Console.WriteLine("Finalized");
}
static void Main()
{
try
{
new Test();
}
catch {}
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
O.K. I'll try it again.
I just copied and ran your program and it worked.
Put the & back for lines 25 and 37...I'm really getting the impression that your guessing about the implementation of scanf. I would try google scanf tutorial.
Try this
scanf("%s",stud1.oname);//remove the &
For Linux Kernel Modules I just use
obj-m += test.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
In your for statement
for(i = 0; i < 10; i++) {
printf("value, deference pointer, address \n");
printf("pointer1 = %p, *pointer1 = %d, &pointer1 = %p\n", pointer1, *pointer1, &pointer1);
pointer1++;//this line should go last
}
Or better yet.
#include <stdio.h>
int main(void)
{
int values[10] = {1,2,3,4,5,6,7,8,9,10};
int *pointer1;
pointer1 = values;
while ( pointer1 < &values[10] )
{
fprintf(stdout, "address->%p, value->%d\n", (void*)pointer1, *pointer1);
++pointer1;
}
return 0;
}
Because the data member is const the = operator can't change its value. So using the = operator on any objects from this class will cause an error.
Your = operator is defined ~ like so
A& A::operator =(const A & obj)
{
if (this != &obj)
{
a = obj.a;//fails here since a is const
}
return *this;
}
Then you have to explicitly refer to the members
public void a.b()
{
// TODO: Add Class1.d implementation
}
Whats so bad about global vareable?
Here, more google nonsense.
http://stackoverflow.com/questions/484635/are-global-variables-bad
Just tried this and it works..
using System;
namespace testit
{
class MainClass
{
public static void Main (string[] args)
{
Console.WriteLine( inputMethod() );
}
public static float inputMethod()
{
float temp;
if (float.TryParse("1234.45".ToString(), out temp))
{
return temp;
}
else
{
return 0.0f;
}
}
}
}
try
float.TryParse(txtInitialTemp.Text, out temp)
Declare the variables globally.i.e before main()
Yikes...Is that good advice?
Try reading up on references or pointers. I would try references first before plunging into pointers.
Simple reference example.
#include <iostream>
void square_it(int & i)//pass by reference
{
i *= i;
}
int main()
{
int i = 5;
std::cout << i << std::endl;
square_it(i);
std::cout << i << std::endl;
return 0;
}
So it seems like I should use a DLL, but how do I use a DLL?
That's operating system specific...Which operating system are we talking about here?
Take a read here
http://www.newty.de/fpt/fpt.html#r_value
Especially the section - 2.7 How to Return a Function Pointer ?
Where's your constructor that accepts three integers?
It should be
fraction fraction::addFractions(fraction two)
{
//...
}
Take a look at your braces in your if statement.
Try this
bool checkNumber(int arr[], int value)
{
for(int i=0; (i < 10); i++)
{
std::cout << arr[i] << std::endl;
if(value == arr[i])
{
return true;
}
}
return false;
}
I would read the contents of this link
http://www.cplusplus.com/reference/string/string/
I would really pay attention when you get to the string's method find.