Saturday, May 22, 2010

[C#] Is using nullable types for every variable a good programming skill?

In C#, is using nullable types for every variable a good programming skill?





For example, instead of setting every int / double to 0, char to \0, or string to "", should I use nullable types to set these variables to null?

[C#] Is using nullable types for every variable a good programming skill?
I would say its not a good idea. Whether you use nullable types depends on if it makes sense for that variable to be null.





If it needs to be created earlier but assigned a value later, then a nullable variable makes sense.





Some times a null value can take on a meaning of its own. In other words null can be just as valid value as an assigned value. Take for example a variable call average_age. What happens if there is no people? is the average 0? No its not. an average of 0 would imply some number of new born people. The best way would be to assign it to null because there really is no value. But null is still a value and no less important than an actual value. Theres a place for this kind of use for null, but it also could be abused.





But now take an example of a variable called number_of_people. Does a null value make any sense here? Well maybe in some circumstances it does, but in most it wouldn't. Just like a negative number wouldn't really make sense either.





Last note: Always write your code so that some one that is not you can look at it and understand what you were thinking. The way you "type" your variables will be something that will help that person understand.
Reply:http://blogs.msdn.com/csharpfaq/archive/...


C++ I have an error that I couldn't figure out T_T?

Here is my program :





#include %26lt;stdio.h%26gt;


#include %26lt;math.h%26gt;


void drain();


int main()


{


double x, y, Y, RE;


printf("\nEnter x: ");


scanf("%lf", %26amp;x);


y = pow(exp(x), 1.5);


printf("True value of exp(1.5*%.4f) = %lf", x, y);


char ch;


fflush(stdin);


printf("\n\nEnter a character 1-4 (0 to exit):\n");


ch = getchar();


switch(ch){


case 1:


printf("1 term(s) approximation"};


Y = pow(x, 0.)/1.;


printf("Approximate exp(1.5*%.4f) = ", x);


RE = 100 * (Y - y)/y;


printf("\nRelative error = %lf", RE);


break;


default:


printf("unrecognized operator");


}


}





.c: In function `main':


:17: error: parse error before '}' token








What should i do? .... thx so much

C++ I have an error that I couldn't figure out T_T?
well, thts easy, u see, u have defined main as returning an integer but u r not returning anything


check out the same program wid the corrections below





#include %26lt;stdio.h%26gt;


#include %26lt;math.h%26gt;


void drain();


int main()


{


double x, y, Y, RE;


printf("\nEnter x: ");


scanf("%lf", %26amp;x);


y = pow(exp(x), 1.5);


printf("True value of exp(1.5*%.4f) = %lf", x, y);


char ch;


fflush(stdin);


printf("\n\nEnter a character 1-4 (0 to exit):\n");


ch = getchar();


switch(ch){


case 1:


printf("1 term(s) approximation"};


Y = pow(x, 0.)/1.;


printf("Approximate exp(1.5*%.4f) = ", x);


RE = 100 * (Y - y)/y;


printf("\nRelative error = %lf", RE);


break;


default:


printf("unrecognized operator");


}


return(0);


}





if my solution was of help, gimme the best answer vote, pls
Reply:My vote goes to this line!


printf("1 term(s) approximation"};


It is line 17!





The end character should be ) NOT }





That was fun.
Reply:switch(ch) { %26lt;!-- put a space between (ch) and {


case 1:


printf("1 term(s) approximation"};


Y = pow(x, 0.)/1.; %26lt;!-- why do u have a point (.) after 1? if its a decimal put 1.0 and why do u havea period after 0?


printf("Approximate exp(1.5*%.4f) = ", x);


RE = 100 * (Y - y)/y;


printf("\nRelative error = %lf", RE);


break;


default:


printf("unrecognized operator");


Using argv in C++ to get ints?

I have a program that has main(int argc, char *argv[])





if say, my line was ./a.out baby 42





I know I can store "baby" in a string by saying





string a=arg[1];


cout%26lt;%26lt;a; //would pring out "baby"





int b=???


cout%26lt;%26lt;b; //I want to print 42





I want to store 42 as in int in C++ but argv takes c-style strings. please help.

Using argv in C++ to get ints?
the arguments (arg[1], arg[2] etc) are of type char *, that's a C string. You have to convert the char* to an integer. There are several ways to do that, i.e. with atoi() or with strtoul().





int i=(int)strtoul(arg[2], 0, 0);





string a=arg[1];


this works, because the compiler knows how to build a std::string from a char*. (There's an overloaded assignment operator which accepts a char * parameter).
Reply:There are lots of ways to do this. I'd use atoi().





int b=atoi(argv[1]);





You might also look at strtoul, for example. Also
Reply:take say you had 342. If you wanted to convert it to an int, you'd take the first character 4 and subtract the character 0 from it.


'3' - '0' = the integer 3, now multiply that times 100.


'4' -'0' = the integer 4, now multiply that times 10.


'2' - '0' = the integer 2, you don't have to multiply this one, now add the three together.





Hope that helps!
Reply:Convert the string "42" into and integer.
Reply:The variable a is declared as a std::string, so it's a real string, not a pointer-to-char.





To convert a string (or anything, really) to a number (or anything, really), I recommend the use of boost::lexical_cast [1]. To use it, you do this (with the example and coding style [2] you had above):





int b = lexical_cast%26lt;int%26gt;(argv[2]);





Of course you can replace the int with, say, a double and it will convert the string (or whatever) to a double.





You will have to install Boost in order to use this highly useful facility, but in my view, that is a small price to pay. You can find installation instructions at [3].


C programming question?

Given the following declarations:


char a[ ] = "abc", *p = "def";





What is the effect of the following statement?





p=a





a. It copies the string "abc" into string p





b. It changes p to point to string "abc"





c. It copies the first character of a ton the first character of p





d. It generates an error

C programming question?
haha cheater.





ya D) is looking good.
Reply:Genrates Error
Reply:b.


p is a pointer to char.


The name of the array a is also a pointer to char.
Reply:b. p will point to a which contains "abc".
Reply:The answer is b.





I just wrote a little code to do it, although I should have been smart and just looked at K%26amp;R.
Reply:p will then point to the first position of a, it won't point to the whole string but you can use pointer arithmatic to get the rest of the string. So - E. none of the above.
Reply:Usually it would be b. Because you replaced the adress the pointer points at. But i am not quite sure you can declare a pointer on a string constant (*p="def") - it would probably generate an error.

flower pots

C++, im taking in int inputs and i have to give out the input but it must be two characters long, please read

ok, so im working with C++ here, and im asking people to input a date in the form mm/dd/yyyy





im using a void function that looks like this:





void output_date (int month, int day, int year)


{


char SLASH = '/';


cout %26lt;%26lt; month %26lt;%26lt; SLASH %26lt;%26lt; day %26lt;%26lt; SLASH %26lt;%26lt; year %26lt;%26lt; endl;


}





when they input the date like this "12/20/2007" the output will look like this "12/20/2007"





but i have to make sure that the 'day' is two digits long, so if they input "12/4/2007" i have to output "12/04/2007"





i only have to do that for the value of 'day'





will someone please help me! i dont know how to make the day number have a '0' infront if it to make it a two digit int, and to not add anything to it if its arleady a two digit int!





p.s. im doing this for a class and i can not change the void function or anything else, we have to only use things we've gone over in class so i can not go and rewrite the whole thing in a different way. it has to be this way or i get an F. i just need a way to put that '0' in there

C++, im taking in int inputs and i have to give out the input but it must be two characters long, please read
void output_date (int month, int day, int year)


{


char SLASH = '/';


cout %26lt;%26lt; month %26lt;%26lt; SLASH;


if (day %26lt; 10) cout %26lt;%26lt; "0";


cout %26lt;%26lt; day %26lt;%26lt; SLASH %26lt;%26lt; year %26lt;%26lt; endl;


}





In programming, you need to SIMPLIFY things and some times look at them in a simple way instead of making them too complex!


Days can consist of either two or one digit(s), right? so they can consist of only one digit if and only if the day is less than 10, right? So you'd make a simple condition in such if the day is less than 10, then add a leading zero, that's all!
Reply:int getInput(char *month) {


month=malloc(10);


*(month+2)=NULL; *(month+1)=NULL;


scanf("%s",month);


if(*(month+2)!=NULL) return 1;


if(*(month+1)==NULL) {


*(month+1)=*month; *(month+1)=0; }


return 0;


}





this wont work as is for year because it is four characters, but u can do it similarly, i think requires header file stdio.h or something like that, look it up if you need to, i am fairly sure that works


C++ program ?

program get string of arbitrary length in char array


then reverse it and print.


we have to use such for loop


for (i=0,j=(strlen());c;i++,j--)


i know that there is command that do it very easier but i need this one.

C++ program ?
well you can do it this way





string str,


int i;


cout%26lt;%26lt;"\n Enter any string to reverse ";


cin%26gt;%26gt;str;


cout%26lt;%26lt;"\n The string in reverse is ";





length=strlen(str);


for(i=length;i%26gt;0;i--)


{


cout%26lt;%26lt;str[i];


}





or you can do it this way





string str,str2,


int i;


cout%26lt;%26lt;"\n Enter any string to reverse ";


cin%26gt;%26gt;str;


str2="";





for(i=length;i%26gt;0;i--)


{


str2=strcat(str2,str[i])


}





cout%26lt;%26lt;"\n The string in reverse is "%26lt;%26lt;str2%26lt;%26lt;endl;











anyway if you want a single line answer then go for strrev command





cout%26lt;%26lt;"\n enter the string to be reveresed ";


cin%26gt;%26gt;str


cout%26lt;%26lt;strrev(str)%26lt;%26lt;endl;





hope it helps
Reply:Superb %26amp; complete answer. Great!!!!!!!!!!!!!!


Keep on answering. Report It



Sunday, August 2, 2009

C++- How do i transfer a long string from file to an array?

I have a .txt file that looks more or less like this:





Name1 (number) (number) (number) Name2 (number) (number) (number) etc....





it is all on one line and each name and number has a space in between the value. I can insert the string into my C++ program but it is one long string. I would like to have it as an array of char or integers so i can use the numbers after each name. Or if anything just get an array of strings like :





string array[10000];


// the array needs to be big because the entries will grow by //one name each time i run my program. But i want to use the //data later. so i figure i can put it into an array of strings.


array[0] = "Name1";


array[1] = (number);


array[2] = (number);


.....and so forth.





i am under the impression that you can use the white spaces in the original string to split it up between each word. Is that right?


Could somebody help me or lead me in the right direction?

C++- How do i transfer a long string from file to an array?
yes, split on the spaces, here's some code...





StringSplit(string str, string delim, vector%26lt;string%26gt; results)


{


int cutAt;


while( (cutAt = str.find_first_of(delim)) != str.npos )


{


if(cutAt %26gt; 0)


{


results.push_back(str.substr(0,cutAt))...


}


str = str.substr(cutAt+1);


}


if(str.length() %26gt; 0)


{


results.push_back(str);


}


}