You can get one new for $550, Im gonna try to find one used though.
Is the Schecter C-5 bass a good five string?
my friend has one and it sounds and feels really good...for 550 thats a good deal
AE
Reply:schecter is a very good brand. i got my ex an a-7 elite guitar for his b-day a few years ago.... beautiful guitar. if you can't find a used c-5, then you should def get a new one... it's worth the money .
Sunday, August 2, 2009
I am using C++ and I need help with a program question dealing with string/function call)?
Let's say I have a function defined as
str_call (int x)
When I do str_call(0), I want the function to return the string
"Ninety"...
How can I write up the function? I have no experience dealing with strings.
I am using C++ and I need help with a program question dealing with string/function call)?
See below for an example of a function that returns a string. The functionality you're looking for calls, I think, for a lookup table. The example also illustrates that.
By the nature of your question, I assume you're something of a beginner in C++. Some of the things you see below may be new to you, but it's never too soon to learn. C++ offers many useful features, and you should learn to take advantage of them.
#include %26lt;iostream%26gt;
#include %26lt;string%26gt;
#include %26lt;map%26gt;
using namespace std;
typedef map%26lt;int,string%26gt; LookupTable;
typedef pair%26lt;int,string%26gt; LookupTableElement;
string translate(int,const LookupTable%26amp;);
int main(int argc, char *argv[]) {
LookupTable lookup;
// Initialize
lookup.insert(LookupTableElement(0,strin...
lookup.insert(LookupTableElement(1,strin...
// ...
lookup.insert(LookupTableElement(90,stri...
// Translate
cout %26lt;%26lt; translate(90,lookup) %26lt;%26lt; endl;
cout %26lt;%26lt; translate(99,lookup) %26lt;%26lt; endl;
return 0;
}
string translate(int x,const LookupTable%26amp; map) {
LookupTable::const_iterator i;
if ((i = map.find(x)) != map.end()) {
return i-%26gt;second;
} else {
return string("not found");
}
}
// Program output:
// ninety
// not found
Reply:I doubt you're dumb, you just haven't learned enough C++ yet. For now, you can skip over the more complicated stuff. The fundamentals of what you asked for - how to return a string from a function - are fairly simple, and clearly shown in my 'translate' function. Report It
Reply:When dealing with strings as output parameters of functions, it's best to provide the function a buffer for filling it up. For example:
bool str_call(int x, char *result, int resultSize)
{
if (x==0)
{
if (resultSize%26gt;=strlen("Ninety")) // check to see if the provided buffer is big enough
{
strcpy(result, "Ninety");
return true; // return a successfull return code
}
}
return false; // result isn't valid
}
void main()
{
char result[100] = { '\0' };
if (str_call(0, result, sizeof(result))==true) // Calling the str_call function
{
printf("%s\n", result);
}
};
// Have fun,
// S. B.
Reply:You have two options. If you only ever want to return the one string, "ninety", then you can do this:
char *str_call(int x)
{
return "ninety";
}
my_other_function()
{
printf ("%s\n", str_call(0)); // Prints "ninety"
}
This is OK, but it's a bit limited. A better option would be to have something like this:
char *str_call(int x, char *storage)
{
strcpy(storage, "ninety");
return storage;
}
my_other_function()
{
char str[20];
printf ("str_call returns %s\n", str_call(0, str)); // Prints "str_call returns ninety"
printf ("Also, str = %s\n", str); // Prints "Also, str = ninety"
}
For completeness it's better to set limits on the size of these arrays, so you'd have this:
char *str_call(int x, char *storage, int maxlen)
{
strncpy(storage, "ninety", maxlen);
return storage;
}
my_other_function()
{
const int strSize = 20;
char str[strSize];
printf ("str_call returns %s\n", str_call(0, str, strSize)); // Prints "str_call returns ninety"
printf ("Also, str = %s\n", str); // Prints "Also, str = ninety"
}
Finally, if you're using C++ then you may as well also use the benefits of STL like this:
void str_call(int x, std::string %26amp;storage)
{
storage = "ninety";
}
my_other_function()
{
std::string str;
str_call(0, str);
printf ("str = %s\n", str); // Prints "str = ninety"
}
Reply:Have a read about pointers.
Strings are arrays of characters. when passing an array, you should pass a pointer the memory address of the first element in the array.
char mystring[50]
char* StringFunction(int x){
char mystring2[50];
strcpy(mystring2, "Ninety");
return mystring2;
}
int main(){
mystring = StringFunction(0);
cout %26lt;%26lt; mystring;
return 0;
}
__
I havnt been learning C++ long, but try compiling that. I can't garuntee it is the most efficient way even if it does work, but it might give you a start
Reply:go to www.freeprogrammersheaven.com and search you can learn and will get the answers
tarot cards
str_call (int x)
When I do str_call(0), I want the function to return the string
"Ninety"...
How can I write up the function? I have no experience dealing with strings.
I am using C++ and I need help with a program question dealing with string/function call)?
See below for an example of a function that returns a string. The functionality you're looking for calls, I think, for a lookup table. The example also illustrates that.
By the nature of your question, I assume you're something of a beginner in C++. Some of the things you see below may be new to you, but it's never too soon to learn. C++ offers many useful features, and you should learn to take advantage of them.
#include %26lt;iostream%26gt;
#include %26lt;string%26gt;
#include %26lt;map%26gt;
using namespace std;
typedef map%26lt;int,string%26gt; LookupTable;
typedef pair%26lt;int,string%26gt; LookupTableElement;
string translate(int,const LookupTable%26amp;);
int main(int argc, char *argv[]) {
LookupTable lookup;
// Initialize
lookup.insert(LookupTableElement(0,strin...
lookup.insert(LookupTableElement(1,strin...
// ...
lookup.insert(LookupTableElement(90,stri...
// Translate
cout %26lt;%26lt; translate(90,lookup) %26lt;%26lt; endl;
cout %26lt;%26lt; translate(99,lookup) %26lt;%26lt; endl;
return 0;
}
string translate(int x,const LookupTable%26amp; map) {
LookupTable::const_iterator i;
if ((i = map.find(x)) != map.end()) {
return i-%26gt;second;
} else {
return string("not found");
}
}
// Program output:
// ninety
// not found
Reply:I doubt you're dumb, you just haven't learned enough C++ yet. For now, you can skip over the more complicated stuff. The fundamentals of what you asked for - how to return a string from a function - are fairly simple, and clearly shown in my 'translate' function. Report It
Reply:When dealing with strings as output parameters of functions, it's best to provide the function a buffer for filling it up. For example:
bool str_call(int x, char *result, int resultSize)
{
if (x==0)
{
if (resultSize%26gt;=strlen("Ninety")) // check to see if the provided buffer is big enough
{
strcpy(result, "Ninety");
return true; // return a successfull return code
}
}
return false; // result isn't valid
}
void main()
{
char result[100] = { '\0' };
if (str_call(0, result, sizeof(result))==true) // Calling the str_call function
{
printf("%s\n", result);
}
};
// Have fun,
// S. B.
Reply:You have two options. If you only ever want to return the one string, "ninety", then you can do this:
char *str_call(int x)
{
return "ninety";
}
my_other_function()
{
printf ("%s\n", str_call(0)); // Prints "ninety"
}
This is OK, but it's a bit limited. A better option would be to have something like this:
char *str_call(int x, char *storage)
{
strcpy(storage, "ninety");
return storage;
}
my_other_function()
{
char str[20];
printf ("str_call returns %s\n", str_call(0, str)); // Prints "str_call returns ninety"
printf ("Also, str = %s\n", str); // Prints "Also, str = ninety"
}
For completeness it's better to set limits on the size of these arrays, so you'd have this:
char *str_call(int x, char *storage, int maxlen)
{
strncpy(storage, "ninety", maxlen);
return storage;
}
my_other_function()
{
const int strSize = 20;
char str[strSize];
printf ("str_call returns %s\n", str_call(0, str, strSize)); // Prints "str_call returns ninety"
printf ("Also, str = %s\n", str); // Prints "Also, str = ninety"
}
Finally, if you're using C++ then you may as well also use the benefits of STL like this:
void str_call(int x, std::string %26amp;storage)
{
storage = "ninety";
}
my_other_function()
{
std::string str;
str_call(0, str);
printf ("str = %s\n", str); // Prints "str = ninety"
}
Reply:Have a read about pointers.
Strings are arrays of characters. when passing an array, you should pass a pointer the memory address of the first element in the array.
char mystring[50]
char* StringFunction(int x){
char mystring2[50];
strcpy(mystring2, "Ninety");
return mystring2;
}
int main(){
mystring = StringFunction(0);
cout %26lt;%26lt; mystring;
return 0;
}
__
I havnt been learning C++ long, but try compiling that. I can't garuntee it is the most efficient way even if it does work, but it might give you a start
Reply:go to www.freeprogrammersheaven.com and search you can learn and will get the answers
tarot cards
In Visual C++, I want to read a string from a text file, and assign it to a variable?
But for some reason I have programmer's block and am not quite sure of how to do it. I guess that's what happens when you've been looking at code for three days straight, all day long. I have attempted to do so by trying to force myself to think creatively, but my attempt is quite obviously flawed.
For instance, I have a variable called "fullscreen_x". I want this variable to be assigned information from a specific point on a specific line of a text file (like a user configuration file).
Basically, I just want to know how to read information from a text file. Thanks
In Visual C++, I want to read a string from a text file, and assign it to a variable?
Fred: A disgrace how you answer straight away with full code; you are encouraging laziness. Give hints and algorithms and let the asker implement them rather than showing up.
For the asker:
one of many variation to solve your problem would be:
1.) Read the file
2.) store the content into a data structure (vector, list ...etc.)
3.) Implement a search module which will iterate through that data structure
4.) Tokenize the search result and format it
5.) Assign that result to the variable
Reply:I do it somewhere in there.....I'm to lasy to pick it out....its part of fstream...that is how you open a file read from it into a string, then output to a file, this was compiled with GNU though...hope it still helps
big_int_calc.cpp
A program that will read in a large integer from a file then preform calculations on the number. It will then output the result into another file.
*/
#include%26lt;iostream%26gt;
#include%26lt;fstream%26gt;
#include"big_int.h"
#include"char_to_int.h"
int main(){
//ask the user for options and store as an integer
int option;
std:: cout %26lt;%26lt; '\n'%26lt;%26lt; "Enter an option: " %26lt;%26lt; '\n';
std::cout %26lt;%26lt; "1. Read a big intger from file and add" %26lt;%26lt; '\n';
std::cout %26lt;%26lt; "2. Enter big integer,and multiply by 10^n" %26lt;%26lt; '\n';
std::cout %26lt;%26lt; "3. Multiply an integer read from file" %26lt;%26lt; '\n';
std:: cin %26gt;%26gt; option;
//big int to store result in
big_int result;
//big int to store input data
big_int in_data1, in_data2;
if(option == 1){
//variable to store the file name
char file_name[20];
//ask the user for the name of the file they would like to read from
std:: cout %26lt;%26lt; "Enter the file name you would like to read a big integer from: ";
//input of the file name
std:: cin %26gt;%26gt; file_name;
//opens the file, if file is not opened error is output
std::ifstream in;
in.open(file_name);
// outputs error if needed
if(!in){
std::cout%26lt;%26lt; "Error cannot open file" %26lt;%26lt; '\n';
};
//read the data from the file into a C-array that is a member of the class big_int
int index = 0; // variable to try and make the array size itself
bool second_num = false; // variable to declare when to start second number input
int i = 0; //i is used as the index, but is also the size of the array for in_data1
while(!in.eof()){
char temp;
in.get(temp); // stores the value of the char read in into temp
if(temp != '\n' %26amp;%26amp; temp != '+'){ //filters new line char and +
if(temp == ';'){ // checks for the second number
second_num = true; //sets the value of second num to true once ; is read
// the size of the first big int will be i -1
i = 0; // resets the index
};
//reads first number
if(second_num == false){
in_data1.num_array[i] = char_to_int(temp); //stores value as integer in big_int data type
++i;
in_data1.set_array_size(i); // sets the size of the array
};
//reads in second number with filters
if(second_num == true %26amp;%26amp; temp != ';'){ //filters out ; that may be at the end of the file
in_data2.num_array[i] = char_to_int(temp); //stores the value as an integer in big_int
++i;
in_data2.set_array_size(i); // sets the value of the array size
};
};
};
// variable that decides if user wants to output to screen or file
int result_option;
if(option == 1){
//calculates result
result = in_data1 + in_data2;
//ask user for the input of the option
std::cout%26lt;%26lt; '\n' %26lt;%26lt; "To output result to screen enter 1, to output to file enter 2: ";
std:: cin %26gt;%26gt; result_option;
result = in_data1 + in_data2;
if(result_option == 1){
std::cout %26lt;%26lt; "The result is: " %26lt;%26lt; '\n';
result.output();
std::cout %26lt;%26lt; '\n';
};
};
// outputs to file if option is 2
if(result_option == 2){
//calculates result
result = in_data1 + in_data2;
//ask the user the name of file to write to
std::cout %26lt;%26lt; "Enter the file name to write to: " %26lt;%26lt; '\n';
char outfile_name[20];
//declares the file to be written to
std::cin %26gt;%26gt;outfile_name;
std::ofstream outfile(outfile_name, std::ios::app);
// format output
outfile %26lt;%26lt; "The result is: " %26lt;%26lt; '\n';
for(int i = 0; i %26lt; result.size(); ++i){
outfile %26lt;%26lt; result.num_array[i];
if( i % 50 == 0 %26amp;%26amp; i %26gt; 0){ //starts new line every 50 char
outfile %26lt;%26lt; '\n';
};
};
// output of ; at end of file
outfile %26lt;%26lt; ';';
};
};
// declares a big int entered int which is entered by the user
big_int entered_int;
if(option == 2){
//ask the user what they would like to do with the result
int output_option;
std::cout %26lt;%26lt; "To output to screen enter 1, to output to file enter 2: ";
std::cin %26gt;%26gt;output_option;
char temp;
char int_in[500];
//inputs the big integer to the big_int class array
int index = 0;
std::cout %26lt;%26lt; "Enter big integer with e to end input: " %26lt;%26lt; '\n';
//uses sential value to end input
while(std::cin%26gt;%26gt; temp){
if(temp == 'e'){
break;
};
//converts the char to integers
entered_int.num_array[index] = char_to_int(temp);
++index;
};
//inputs the value of n
entered_int.set_array_size(index);
std::cout %26lt;%26lt; "Enter n (10^n) n=";
int n;
std::cin%26gt;%26gt; n;
for(int i = 0; i %26lt; n; ++i){
//adds 0's on the end of the array
entered_int.num_array[entered_int.size()... = 0;
};
// sets new array size
entered_int.set_array_size(entered_int.s... + n);
if(output_option == 1){
std::cout %26lt;%26lt; "The result is: " %26lt;%26lt; '\n';
entered_int.output();
};
if(output_option == 2){
//writes to file if user chooses that optioin
std::cout %26lt;%26lt; "Enter the file name to write to: " %26lt;%26lt; '\n';
char outfile_name[20];
std::cin %26gt;%26gt;outfile_name;
std::ofstream outfile(outfile_name, std::ios::app);
//formats output with 50 char per line
outfile %26lt;%26lt; "The result is: " %26lt;%26lt; '\n';
for(int i = 0; i %26lt; entered_int.size(); ++i){
outfile %26lt;%26lt; entered_int.num_array[i];
if( i % 50 == 0 %26amp;%26amp; i %26gt; 0){
outfile %26lt;%26lt; '\n';
};
};
//adds ; at end of file
outfile %26lt;%26lt; ';';
};
};
// variable that decides if user wants to output to screen or file
int result_option;
if(option == 3){
//variable to store the file name
char file_name[20];
//ask the user for the name of the file they would like to read from
std:: cout %26lt;%26lt; "Enter the file name you would like to read a big integer from: ";
//input of the file name
std:: cin %26gt;%26gt; file_name;
//opens the file, if file is not opened error is output
std::ifstream in;
in.open(file_name);
// outputs error if needed
if(!in){
std::cout%26lt;%26lt; "Error cannot open file" %26lt;%26lt; '\n';
};
//read the data from the file into a C-array that is a member of the class big_int
int index = 0; // variable to try and make the array size itself
bool second_num = false; // variable to declare when to start second number input
int i = 0; //i is used as the index, but is also the size of the array for in_data1
while(!in.eof()){
char temp;
in.get(temp); // stores the value of the char read in into temp
if(temp != '\n' %26amp;%26amp; temp != '+'){ //filters new line char and +
if(temp == ';'){ // checks for the second number
second_num = true; //sets the value of second num to true once ; is read
// the size of the first big int will be i -1
i = 0; // resets the index
};
//reads first number
if(second_num == false){
in_data1.num_array[i] = char_to_int(temp); //stores value as integer in big_int data type
++i;
in_data1.set_array_size(i); // sets the size of the array
};
//reads in second number with filters
if(second_num == true %26amp;%26amp; temp != ';'){ //filters out ; that may be at the end of the file
in_data2.num_array[i] = char_to_int(temp); //stores the value as an integer in big_int
++i;
in_data2.set_array_size(i); // sets the value of the array size
};
};
};
//calculates result
result = in_data1 * in_data2;
//ask user for the input of the option
std::cout%26lt;%26lt; '\n' %26lt;%26lt; "To output result to screen enter 1, to output to file enter 2: ";
std:: cin %26gt;%26gt; result_option;
if(result_option == 1){
std::cout %26lt;%26lt; "The result is: " %26lt;%26lt; '\n';
result.output();
std::cout %26lt;%26lt; '\n';
};
};
// outputs to file if option is 2
if(result_option == 2){
//calculates result
result = in_data1 + in_data2;
//ask the user the name of file to write to
std::cout %26lt;%26lt; "Enter the file name to write to: " %26lt;%26lt; '\n';
char outfile_name[20];
//declares the file to be written to
std::cin %26gt;%26gt;outfile_name;
std::ofstream outfile(outfile_name, std::ios::app);
// format output
outfile %26lt;%26lt; "The result is: " %26lt;%26lt; '\n';
for(int i = 0; i %26lt; result.size(); ++i){
outfile %26lt;%26lt; result.num_array[i];
if( i % 50 == 0 %26amp;%26amp; i %26gt; 0){ //starts new line every 50 char
outfile %26lt;%26lt; '\n';
};
};
// output of ; at end of file
outfile %26lt;%26lt; ';';
};
/*
if(option == 4){
std::cout %26lt;%26lt; "Input a number to calculate the factorial of: ";
int fact_in;
std::cin %26gt;%26gt; fact_in;
big_int result;
result = fact(fact_in);
std::cout %26lt;%26lt; '\n' %26lt;%26lt; "Enter 1 to output to screen, enter 2 to output to file: " ;
int result_option;
std:: cin %26gt;%26gt; result_option;
if(result_option == 1){
result.output();
};
// outputs to file if option is 2
if(result_option == 2){
//ask the user the name of file to write to
std::cout %26lt;%26lt; "Enter the file name to write to: " %26lt;%26lt; '\n';
char outfile_name[20];
//declares the file to be written to
std::cin %26gt;%26gt;outfile_name;
std::ofstream outfile(outfile_name, std::ios::app);
// format output
outfile %26lt;%26lt; "The result is: " %26lt;%26lt; '\n';
for(int i = 0; i %26lt; result.size(); ++i){
outfile %26lt;%26lt; result.num_array[i];
if( i % 50 == 0 %26amp;%26amp; i %26gt; 0){ //starts new line every 50 char
outfile %26lt;%26lt; '\n';
};
};
// output of ; at end of file
outfile %26lt;%26lt; ';';
};
};
*/
return 0;
};
For instance, I have a variable called "fullscreen_x". I want this variable to be assigned information from a specific point on a specific line of a text file (like a user configuration file).
Basically, I just want to know how to read information from a text file. Thanks
In Visual C++, I want to read a string from a text file, and assign it to a variable?
Fred: A disgrace how you answer straight away with full code; you are encouraging laziness. Give hints and algorithms and let the asker implement them rather than showing up.
For the asker:
one of many variation to solve your problem would be:
1.) Read the file
2.) store the content into a data structure (vector, list ...etc.)
3.) Implement a search module which will iterate through that data structure
4.) Tokenize the search result and format it
5.) Assign that result to the variable
Reply:I do it somewhere in there.....I'm to lasy to pick it out....its part of fstream...that is how you open a file read from it into a string, then output to a file, this was compiled with GNU though...hope it still helps
big_int_calc.cpp
A program that will read in a large integer from a file then preform calculations on the number. It will then output the result into another file.
*/
#include%26lt;iostream%26gt;
#include%26lt;fstream%26gt;
#include"big_int.h"
#include"char_to_int.h"
int main(){
//ask the user for options and store as an integer
int option;
std:: cout %26lt;%26lt; '\n'%26lt;%26lt; "Enter an option: " %26lt;%26lt; '\n';
std::cout %26lt;%26lt; "1. Read a big intger from file and add" %26lt;%26lt; '\n';
std::cout %26lt;%26lt; "2. Enter big integer,and multiply by 10^n" %26lt;%26lt; '\n';
std::cout %26lt;%26lt; "3. Multiply an integer read from file" %26lt;%26lt; '\n';
std:: cin %26gt;%26gt; option;
//big int to store result in
big_int result;
//big int to store input data
big_int in_data1, in_data2;
if(option == 1){
//variable to store the file name
char file_name[20];
//ask the user for the name of the file they would like to read from
std:: cout %26lt;%26lt; "Enter the file name you would like to read a big integer from: ";
//input of the file name
std:: cin %26gt;%26gt; file_name;
//opens the file, if file is not opened error is output
std::ifstream in;
in.open(file_name);
// outputs error if needed
if(!in){
std::cout%26lt;%26lt; "Error cannot open file" %26lt;%26lt; '\n';
};
//read the data from the file into a C-array that is a member of the class big_int
int index = 0; // variable to try and make the array size itself
bool second_num = false; // variable to declare when to start second number input
int i = 0; //i is used as the index, but is also the size of the array for in_data1
while(!in.eof()){
char temp;
in.get(temp); // stores the value of the char read in into temp
if(temp != '\n' %26amp;%26amp; temp != '+'){ //filters new line char and +
if(temp == ';'){ // checks for the second number
second_num = true; //sets the value of second num to true once ; is read
// the size of the first big int will be i -1
i = 0; // resets the index
};
//reads first number
if(second_num == false){
in_data1.num_array[i] = char_to_int(temp); //stores value as integer in big_int data type
++i;
in_data1.set_array_size(i); // sets the size of the array
};
//reads in second number with filters
if(second_num == true %26amp;%26amp; temp != ';'){ //filters out ; that may be at the end of the file
in_data2.num_array[i] = char_to_int(temp); //stores the value as an integer in big_int
++i;
in_data2.set_array_size(i); // sets the value of the array size
};
};
};
// variable that decides if user wants to output to screen or file
int result_option;
if(option == 1){
//calculates result
result = in_data1 + in_data2;
//ask user for the input of the option
std::cout%26lt;%26lt; '\n' %26lt;%26lt; "To output result to screen enter 1, to output to file enter 2: ";
std:: cin %26gt;%26gt; result_option;
result = in_data1 + in_data2;
if(result_option == 1){
std::cout %26lt;%26lt; "The result is: " %26lt;%26lt; '\n';
result.output();
std::cout %26lt;%26lt; '\n';
};
};
// outputs to file if option is 2
if(result_option == 2){
//calculates result
result = in_data1 + in_data2;
//ask the user the name of file to write to
std::cout %26lt;%26lt; "Enter the file name to write to: " %26lt;%26lt; '\n';
char outfile_name[20];
//declares the file to be written to
std::cin %26gt;%26gt;outfile_name;
std::ofstream outfile(outfile_name, std::ios::app);
// format output
outfile %26lt;%26lt; "The result is: " %26lt;%26lt; '\n';
for(int i = 0; i %26lt; result.size(); ++i){
outfile %26lt;%26lt; result.num_array[i];
if( i % 50 == 0 %26amp;%26amp; i %26gt; 0){ //starts new line every 50 char
outfile %26lt;%26lt; '\n';
};
};
// output of ; at end of file
outfile %26lt;%26lt; ';';
};
};
// declares a big int entered int which is entered by the user
big_int entered_int;
if(option == 2){
//ask the user what they would like to do with the result
int output_option;
std::cout %26lt;%26lt; "To output to screen enter 1, to output to file enter 2: ";
std::cin %26gt;%26gt;output_option;
char temp;
char int_in[500];
//inputs the big integer to the big_int class array
int index = 0;
std::cout %26lt;%26lt; "Enter big integer with e to end input: " %26lt;%26lt; '\n';
//uses sential value to end input
while(std::cin%26gt;%26gt; temp){
if(temp == 'e'){
break;
};
//converts the char to integers
entered_int.num_array[index] = char_to_int(temp);
++index;
};
//inputs the value of n
entered_int.set_array_size(index);
std::cout %26lt;%26lt; "Enter n (10^n) n=";
int n;
std::cin%26gt;%26gt; n;
for(int i = 0; i %26lt; n; ++i){
//adds 0's on the end of the array
entered_int.num_array[entered_int.size()... = 0;
};
// sets new array size
entered_int.set_array_size(entered_int.s... + n);
if(output_option == 1){
std::cout %26lt;%26lt; "The result is: " %26lt;%26lt; '\n';
entered_int.output();
};
if(output_option == 2){
//writes to file if user chooses that optioin
std::cout %26lt;%26lt; "Enter the file name to write to: " %26lt;%26lt; '\n';
char outfile_name[20];
std::cin %26gt;%26gt;outfile_name;
std::ofstream outfile(outfile_name, std::ios::app);
//formats output with 50 char per line
outfile %26lt;%26lt; "The result is: " %26lt;%26lt; '\n';
for(int i = 0; i %26lt; entered_int.size(); ++i){
outfile %26lt;%26lt; entered_int.num_array[i];
if( i % 50 == 0 %26amp;%26amp; i %26gt; 0){
outfile %26lt;%26lt; '\n';
};
};
//adds ; at end of file
outfile %26lt;%26lt; ';';
};
};
// variable that decides if user wants to output to screen or file
int result_option;
if(option == 3){
//variable to store the file name
char file_name[20];
//ask the user for the name of the file they would like to read from
std:: cout %26lt;%26lt; "Enter the file name you would like to read a big integer from: ";
//input of the file name
std:: cin %26gt;%26gt; file_name;
//opens the file, if file is not opened error is output
std::ifstream in;
in.open(file_name);
// outputs error if needed
if(!in){
std::cout%26lt;%26lt; "Error cannot open file" %26lt;%26lt; '\n';
};
//read the data from the file into a C-array that is a member of the class big_int
int index = 0; // variable to try and make the array size itself
bool second_num = false; // variable to declare when to start second number input
int i = 0; //i is used as the index, but is also the size of the array for in_data1
while(!in.eof()){
char temp;
in.get(temp); // stores the value of the char read in into temp
if(temp != '\n' %26amp;%26amp; temp != '+'){ //filters new line char and +
if(temp == ';'){ // checks for the second number
second_num = true; //sets the value of second num to true once ; is read
// the size of the first big int will be i -1
i = 0; // resets the index
};
//reads first number
if(second_num == false){
in_data1.num_array[i] = char_to_int(temp); //stores value as integer in big_int data type
++i;
in_data1.set_array_size(i); // sets the size of the array
};
//reads in second number with filters
if(second_num == true %26amp;%26amp; temp != ';'){ //filters out ; that may be at the end of the file
in_data2.num_array[i] = char_to_int(temp); //stores the value as an integer in big_int
++i;
in_data2.set_array_size(i); // sets the value of the array size
};
};
};
//calculates result
result = in_data1 * in_data2;
//ask user for the input of the option
std::cout%26lt;%26lt; '\n' %26lt;%26lt; "To output result to screen enter 1, to output to file enter 2: ";
std:: cin %26gt;%26gt; result_option;
if(result_option == 1){
std::cout %26lt;%26lt; "The result is: " %26lt;%26lt; '\n';
result.output();
std::cout %26lt;%26lt; '\n';
};
};
// outputs to file if option is 2
if(result_option == 2){
//calculates result
result = in_data1 + in_data2;
//ask the user the name of file to write to
std::cout %26lt;%26lt; "Enter the file name to write to: " %26lt;%26lt; '\n';
char outfile_name[20];
//declares the file to be written to
std::cin %26gt;%26gt;outfile_name;
std::ofstream outfile(outfile_name, std::ios::app);
// format output
outfile %26lt;%26lt; "The result is: " %26lt;%26lt; '\n';
for(int i = 0; i %26lt; result.size(); ++i){
outfile %26lt;%26lt; result.num_array[i];
if( i % 50 == 0 %26amp;%26amp; i %26gt; 0){ //starts new line every 50 char
outfile %26lt;%26lt; '\n';
};
};
// output of ; at end of file
outfile %26lt;%26lt; ';';
};
/*
if(option == 4){
std::cout %26lt;%26lt; "Input a number to calculate the factorial of: ";
int fact_in;
std::cin %26gt;%26gt; fact_in;
big_int result;
result = fact(fact_in);
std::cout %26lt;%26lt; '\n' %26lt;%26lt; "Enter 1 to output to screen, enter 2 to output to file: " ;
int result_option;
std:: cin %26gt;%26gt; result_option;
if(result_option == 1){
result.output();
};
// outputs to file if option is 2
if(result_option == 2){
//ask the user the name of file to write to
std::cout %26lt;%26lt; "Enter the file name to write to: " %26lt;%26lt; '\n';
char outfile_name[20];
//declares the file to be written to
std::cin %26gt;%26gt;outfile_name;
std::ofstream outfile(outfile_name, std::ios::app);
// format output
outfile %26lt;%26lt; "The result is: " %26lt;%26lt; '\n';
for(int i = 0; i %26lt; result.size(); ++i){
outfile %26lt;%26lt; result.num_array[i];
if( i % 50 == 0 %26amp;%26amp; i %26gt; 0){ //starts new line every 50 char
outfile %26lt;%26lt; '\n';
};
};
// output of ; at end of file
outfile %26lt;%26lt; ';';
};
};
*/
return 0;
};
How far from the end of the string should you place your finger to play the note C (523 Hz)?
A violin string is 28 cm long. It sounds the musical note A (440 Hz) when played without fingering.
How far from the end of the string should you place your finger to play the note C (523 Hz)?
There are two equations you'll need.
First, when you play a note on a string, the fundamental frequency causes a standing wave such that the length of the string equals 1/2 of a wavelength. So L = 1/2 wavelength.
Given the legnth of the string above (.28 m), you can figure out the wavelength.
.28 m = 1/2 wavelength
.56 m = wavelength
Second, velocity = frequency x wavelength.
Using the frequency given (440 Hz) and the wavelength just computed, you can find the velocity.
velocity = 440 Hz x .56 m = 246.4 m/s
This speed won't change even as you hold your finger on the string.
Starting with a velocity of 246.4 m/s and a desired frequency of 523 Hz, we can find the wavelength.
velocity = frequency x wavelength
246.4 m/s = 523 Hz x wavelength
.471 m = wavelength
Using this wavelength and the fact that the length of the string is 1/2 of a wavelength long, you get:
Length of string = 1/2 wavelength
Length of string = 1/2 (.471 m)
Length of string = .236 m
Since the question asks how far from the end you should place your finger, take the original string length and subtract the answer above.
distance from end = .28 m - .236 m = .044 m or 4.4 cm.
How far from the end of the string should you place your finger to play the note C (523 Hz)?
There are two equations you'll need.
First, when you play a note on a string, the fundamental frequency causes a standing wave such that the length of the string equals 1/2 of a wavelength. So L = 1/2 wavelength.
Given the legnth of the string above (.28 m), you can figure out the wavelength.
.28 m = 1/2 wavelength
.56 m = wavelength
Second, velocity = frequency x wavelength.
Using the frequency given (440 Hz) and the wavelength just computed, you can find the velocity.
velocity = 440 Hz x .56 m = 246.4 m/s
This speed won't change even as you hold your finger on the string.
Starting with a velocity of 246.4 m/s and a desired frequency of 523 Hz, we can find the wavelength.
velocity = frequency x wavelength
246.4 m/s = 523 Hz x wavelength
.471 m = wavelength
Using this wavelength and the fact that the length of the string is 1/2 of a wavelength long, you get:
Length of string = 1/2 wavelength
Length of string = 1/2 (.471 m)
Length of string = .236 m
Since the question asks how far from the end you should place your finger, take the original string length and subtract the answer above.
distance from end = .28 m - .236 m = .044 m or 4.4 cm.
What's wrong with my C#.net code ,im trying to join a bunch of string and integers together.?
udate.Text = "Hello, " + DateTime.Now.DayOfWeek.ToString().Substr... 3) %26amp; " " %26amp; DateTime.Now.Day %26amp; ", " %26amp; definemonth() %26amp; " : You are not logged in.";
The error is at "Hello, " part it said ''Operator '%26amp;' cannot be applied to operands of type 'string' and 'string"
What's wrong with my C#.net code ,im trying to join a bunch of string and integers together.?
Concatenation in C# is done with + not %26amp;
The error is at "Hello, " part it said ''Operator '%26amp;' cannot be applied to operands of type 'string' and 'string"
What's wrong with my C#.net code ,im trying to join a bunch of string and integers together.?
Concatenation in C# is done with + not %26amp;
In Winapi (Visual C++) , how do i get a string "EQ1" from my string list into my resource box "QUESTIONBOX" ?
My current code is:
SetDlgItemText (hwnd, QUESTIONBOX, EQ1)
It returns an error, saying that it cannot convert from const int to const char :S
Hmm wat if i use SetWindowText..
Could someone paste in the exact code i need to type to make this work. thanks.
In Winapi (Visual C++) , how do i get a string "EQ1" from my string list into my resource box "QUESTIONBOX" ?
The following link from Microsoft shows an example on
how to use "SetDlgItemText"
http://msdn.microsoft.com/library/defaul...
SetDlgItemText(hDlg, IDS_POS, Roster[i].tchPosition);
May be this can help you
secret garden
SetDlgItemText (hwnd, QUESTIONBOX, EQ1)
It returns an error, saying that it cannot convert from const int to const char :S
Hmm wat if i use SetWindowText..
Could someone paste in the exact code i need to type to make this work. thanks.
In Winapi (Visual C++) , how do i get a string "EQ1" from my string list into my resource box "QUESTIONBOX" ?
The following link from Microsoft shows an example on
how to use "SetDlgItemText"
http://msdn.microsoft.com/library/defaul...
SetDlgItemText(hDlg, IDS_POS, Roster[i].tchPosition);
May be this can help you
secret garden
C program, use recursive method find string length?
why do I get the feeling that this sounds like a homework assignment? I am giving a very small code that demonstrates the principle. I absolutely will not recommend something like this for any form of deployment
#include %26lt;stdio.h%26gt;
int findlen(char * s) {
int len;
if (*s == '\0') {
return 0;
}
len = findlen(s+1);
return len+1;
}
int main() {
char str[25]="hello world test";
int len = findlen(str);
printf("length = %d\n", len);
}
C program, use recursive method find string length?
U will find the code here:
http://www.codeguru.com/forum/showthread...
Reply:Strings in c are represented as some series of ascii characters followed by the NULL ascii character (represented numerically as 0). So, to do it recursively, our else case increments some variable (the length of the string) while our base case checks to see if the current location in the string is 0 or not. If so, we are done.
//assume we have the value at pointer length is initialized to 0
char strlen (char* s, int* length) {
if (!*s) {
return;
}
else{
*length++;
strlen(s++,length);
}
}
It could be written more efficiently, but you get the idea.
Reply:See here, you may find the answer: http://www.codeproject.com
#include %26lt;stdio.h%26gt;
int findlen(char * s) {
int len;
if (*s == '\0') {
return 0;
}
len = findlen(s+1);
return len+1;
}
int main() {
char str[25]="hello world test";
int len = findlen(str);
printf("length = %d\n", len);
}
C program, use recursive method find string length?
U will find the code here:
http://www.codeguru.com/forum/showthread...
Reply:Strings in c are represented as some series of ascii characters followed by the NULL ascii character (represented numerically as 0). So, to do it recursively, our else case increments some variable (the length of the string) while our base case checks to see if the current location in the string is 0 or not. If so, we are done.
//assume we have the value at pointer length is initialized to 0
char strlen (char* s, int* length) {
if (!*s) {
return;
}
else{
*length++;
strlen(s++,length);
}
}
It could be written more efficiently, but you get the idea.
Reply:See here, you may find the answer: http://www.codeproject.com
Subscribe to:
Posts (Atom)