Friday, July 31, 2009

C++ array to hold full name (first last) as a single string in array?

I need to store the full name, first and last name in an array as a single string, and its corresponding value in a parallel array. This means to store the full name in one location in the array. Using C++ and my compiler is Dev C++. I need help with this array primarily how to store the name as one unit and the value in a corresponding location in a parallel array. Here is my code so far:





#include%26lt;iostream%26gt;


#include%26lt;string%26gt;


#include%26lt;cstring%26gt;


#include%26lt;iomanip%26gt;


#include%26lt;fstream%26gt;





using namespace std;








int main()





{





ifstream inFile;





char array1 [10] [50];


char array2 [10] [5];





inFile.open("celebs.txt");





int location = 0 ;





inFile %26gt;%26gt; array1[ location ] %26gt;%26gt; array2[ location ] ;





while ( inFile %26amp;%26amp; location %26lt; 50 )


{


location++;


inFile %26gt;%26gt; array1[ location ] %26gt;%26gt; array2[ location ] ;





}





this is the input file:





Donald Trump 5.1


Paris Hilton 2.98


Lindsey Lohan 3.65


Marion Jones 1.03


Paul McCartney 3.75


Snoop Dogg 1.65


Britney Spears 2.14


Sean Combs 1.07


Heather Mills 2.31


Michael Vick 4.67

C++ array to hold full name (first last) as a single string in array?
Since this is C++, you can take advantage of string methods. I recommend that you scan backward in your input for the last separating space and split the input at that point. Also, instead of two-dimensional char arrays, why not use array of strings? See below for my attempt:





#include%26lt;iostream%26gt;


#include%26lt;string%26gt;


#include%26lt;cstring%26gt;


#include%26lt;iomanip%26gt;


#include%26lt;fstream%26gt;





using namespace std;





const int TotalRows = 10;





int main()





{





ifstream inFile;





string input_string, array1[TotalRows], array2[TotalRows];





inFile.open("celebs.txt");





int location = 0, linecount = 0 ;





while ( linecount %26lt; TotalRows )


{


getline(inFile, input_string);


location = input_string.rfind(" ");


array1[linecount] = str.substr(0, location);


array2[linecount] = str.substr(location+1);


linecount++;


}


inFile.close();





}


C Programming - Reading input from STDIN - need help.?

I need to write a program in pure C. Coming from a C++/Java background I am slightly confused.





I would like to read in a float value from STDIN. I see there are functions like read(), scanf().... that will let you read in char[], but nothing to read in something else.





So how can a read in a float, or how would I convert a char[] to a float.

C Programming - Reading input from STDIN - need help.?
#include %26lt;stdio.h%26gt;





int main() {


float x;


printf("Enter a float value for x: ");


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


return 0;


}





printf will put the string give to the standard output (this case the console).


scanf will read from the standard input.. Because it accepts any number of parameters (which are adresses to variables declared in the program) you must specify what to expect in the string given at the input. %f means here you must read a float.. If I added %f %d for example it meant fisrt try to read a float until you find a blank then try to read an integer...


printf also accepts any number of parameters and if you want to show variables you have to do exactly the same thing as for scanf.. So if I were to show the x value I would write


printf("The value for x is : %f",x); (instead of %f it will put the float value of x... if x was declared as an integer it will automatically convert it to float and then show it- implicit conversion)... and so on. :)


You'll find the documentation for printf fprintf, scanf fscanf and lots more on the internet... (look here : http://www.cprogramming.com/)

survey for cash

C string manipulation?

Hello, I am trying to write c function that will allow me to input 2 arguments in stdin in which the first argument will translate into argument 2.





Example : abc(argument 1) xyz(argument 2)


a -%26gt; x


b -%26gt; y


c -%26gt; z





If you're familiar with unix, you might recognize this as the tr utility.





What I have so far is this:





int translate(char str1[], char str2[]){





while ((str1[] = getchar()) != EOF)


if (str1[] == str2[])


putchar(str2[]);


else


putchar(str1[]);


}





I'm not really sure if that's correct because I just used the standard while loop for copying a single character and replaced it with an array instead.





Any help would be appreciated.

C string manipulation?
First, there is no evidence that the two str?[] arrays are 4 or greater. Let us assume they are and that you have a means for error checking.


2. EOF !=NULL. NULL ('\0') is usually used to terminate arrays of char such as we use as strings. As I read this routine, your routine accepts input into str1 until it receives a Ctl-D (in Unix) or a Ctl-Z (in Windoze). It checks to see if str1 is equal to str2. If it is it writes str2 to the screen, or else it writes str1 to the screen. There is no length checking or anything else.





I would separate each and every action a bit more. These "arrays" function as chars. Without actually checking I see nothing to increment their positions, and the only place that would be affected would be str1[0] which would be written over and written over until EOF was received.





While no expert I just had to use tr for housekeeping the other day because I'm way too fancy with my directories and that's not how it functions. Remember, if you process a string with no NULL termination you risk a segmentation fault.


Important C Question!!!!?

I have a array of strings


char ** array;





I need to pass this array to a functio n by reference





func ( char *** arr);





is that a valid way to do it??





Also I need to add new elements to this array.





when i return to main how do i find the number of strings in the array


e.g


main(){


char ** array;


func(%26amp;arr);


// How do i find the length of 'array' after the func has initialised it ???





if you guys have any suggestions please help out!!








I am new to C!!

Important C Question!!!!?
You pass a pointer to a return variable to contain the number of elements. There is no way to discover the number.


Trivial C++ string question?

In certain compilers ( possibly only dev- C++, I didn't try on anything else), it is possible to declare a string variable with including %26lt;string%26gt; header file. Why is this possible?


Could anyone inform me about the uses of the string header file and whether or not it is required to declare strings variables. In addition,I have heard that a string is just an array of char, so what is the difference between declaring an array of char and declaring a string. Is it more advisable to use an array of char rather than using a string? And what are the possible advantages and disadvantages of doing so?

Trivial C++ string question?
when u include the header %26lt; string%26gt; n then use a string class to declare a string variable then u can use the string modification functions available withthat particular header ...


When u declare a string as an array of characters u dont get this functionality ...


the difference between declaring a string as an object of string class this is like


#include%26lt;string%26gt;


.


.


.


string sample = "blah blah";


here the sample is a simple object of type "string" which is actually a class whose declaration n definition is in the header file. so ther might be some function available with the class definition that u can use like


length()


substring(..)


reverse()


toUpper()


etc etc


where as declaring like :





char sample[] .......


indicates that the sample is an array of type char.


No functions available by default unless u use some other header with char array modificating functions ..


here sample has an extra character /0 indicating the end of string which is not the case if u use an object of string class.


I hope i explained it sufficiently for u .
Reply:1. Dev C++ is an IDE, not a compiler. I think it uses mingw as its compiler (at least wxDevCpp does)





Look into wxDevCPP. http://wxdsgn.sourceforge.net/





2.You need the string header to use the string data type.





3. The string data type is better because you don't have to write a bunch of string manipulation functions and because the string data type prevents buffer overruns because it knows its own size.
Reply:In C++ there is no data type called string


all you have is char array only





moreover string header file is used to manipulate some string operations to find string length, to find reverse of the string and to compare two strings ..etc





without using the string header files also you can perform string related operations in C++ (but you have to write methods for that)





I think now you are clear
Reply:As explained by "blithe-lad" there are lot of additional functionalities with string object.





Though there is no string class in C++, it is part of stl (stadard template library) and widely used.





This is very handy especially when you do lot of string manipulations with the data. If your compiler supports stl it will have the support for string.


C++ study guide help?

1.Which of the following can contain data items of different types?


a.an array


b.a structure


c.a string


d.none of the above; they can only contain different values


2.For a main function defined as following:


void main(int argc, char * argv[])


Which statement below is NOT correct?


a.The first argument tells how many separate arguments were passed.


b.The second argument represents an array of strings.


c.The first (i.e. the 0th) argument is the name of the program


d.The first (i.e. the 1st) argument is the name of the program


e.none of the above


3.In C++, all variables


a.take up the same amount of storage on a certain computer type


b.take up the same amount of storage on all computer types


c.take up different amounts of storage depending on the data type


d.take up different amounts of storage depending on the length of the variable name


4.Every element of any array takes up one byte in memory


a.true


b.false


5.In object oriented programming the constructor


a.is the method called when you create an instance of that class


b.has the same name as the name of the class


c.is used to initialize variables


d.all of the above

C++ study guide help?
1-a , 2-e(maybe) 3-c 4-b 5-d

customer satisfaction survey

Please help!!! C++ user defined funtions?

im trying to put some functions into my code. i want to create 2 functions in my switch statement one for case A and one for case B but im having trouble with my variables and the user input here is my code im open to any suggestions








#include %26lt;iostream%26gt;





using namespace std;





int main()


{





char choice; // variable for selection


int quantity; // variable for quantity of numbers


int num1; // variable for option A numbers


int num2; // variable for option B numbers


int largest; // variable for largest number


int smallest; // variable for smallest number





do


{ // beginning of do while loop





cout %26lt;%26lt; " Please choose one of the following options" %26lt;%26lt; endl; // instructions telling user what to enter





cout %26lt;%26lt; " A. - find the largest number with a known quantity of numbers" %26lt;%26lt; endl; //


cout %26lt;%26lt; " B. - Find the smallest number with an unknown quantity of numbers" %26lt;%26lt; endl; // display menu options to user


cout %26lt;%26lt; " C. - Quit" %26lt;%26lt; endl; //





cout %26lt;%26lt; " Please enter your choice: - "; //


cin %26gt;%26gt; choice; // ask user for input


cout %26lt;%26lt; endl; //





switch (choice)


{ // beginning of switch statement





case 'a': // case for when user selects option A


case 'A':





cout %26lt;%26lt; " A. - find the largest number with a known quantity of numbers" %26lt;%26lt; endl;


cout %26lt;%26lt; " Please enter the total quantity of numbers you would like to use: ";


cin %26gt;%26gt; quantity;





for (quantity;quantity %26gt; 0;quantity--)


{





cout %26lt;%26lt; " please enter a number - ";


cin %26gt;%26gt; largest;


cout %26lt;%26lt; endl;





for (quantity;quantity %26gt; 1;quantity--)


{


cout %26lt;%26lt; " please enter a number - ";


cin %26gt;%26gt; num1;


cout %26lt;%26lt; endl;





if (largest %26lt;= num1)


largest = num1;


}





}





cout %26lt;%26lt; " the largest number you entered is (" %26lt;%26lt; largest %26lt;%26lt; ")" %26lt;%26lt; endl;





break;





case 'b': // case for when user selects option B


case 'B':





cout %26lt;%26lt; " B. - Find the smallest number with an unknown quantity of numbers" %26lt;%26lt; endl;





cout %26lt;%26lt; " enter your first number - ";


cin %26gt;%26gt; num2;


cout %26lt;%26lt; endl;





smallest = num2;





cout %26lt;%26lt; " to get result and return to main menu enter (-99) for your number" %26lt;%26lt; endl;





while (num2 != -99)


{





cout %26lt;%26lt; " please enter another number - ";


cin %26gt;%26gt; num2;


cout %26lt;%26lt; endl;





if (num2 != -99)


if (num2 %26lt; smallest)


smallest = num2;


}





cout %26lt;%26lt; " the smallest number you entered is (" %26lt;%26lt; smallest %26lt;%26lt; ")" %26lt;%26lt; endl;





break;





case 'c': // case for when user selects to exit the program


case 'C':





exit('c');





default: // case for when user tries to select and option other than a, A, b, B, c, or C





cout %26lt;%26lt; " the letter you entered is not valid" %26lt;%26lt; endl;





} // end of switch statement





} while (choice != 'c'); // end of do while loop





return 0;





}

Please help!!! C++ user defined funtions?
I see a problem with this section of code:


You are reading in a value for largest. That should only change when num1 is larger than largest's current value.





for (quantity;quantity %26gt; 0;quantity--)


{





cout %26lt;%26lt; " please enter a number - ";


cin %26gt;%26gt; largest;


cout %26lt;%26lt; endl;





for (quantity;quantity %26gt; 1;quantity--)


{


cout %26lt;%26lt; " please enter a number - ";


cin %26gt;%26gt; num1;


cout %26lt;%26lt; endl;





if (largest %26lt;= num1)


largest = num1;


}





}
Reply:create 2 functions called


void largest()


{


do all your things here;


returns nothing;


}


void smallest()


{


do all your things here;


returns nothing;


}