Use the following structure to set up a dictionary:
struct dictionary
{
char word[10];
char definition[80];
int noun_or_verb; /* 1 for noun 2 for verb */
};
Your dictionary will initially have at least 5 words in it, with room for a maximum of 10 words. You decide on the words and their definitions.
Have the user input a word. Test to see if the word is in the dictionary. If it is in the dictionary, display it to the screen in the following format:
the word - N (for noun) or V (for verb) - definition
For example:
sweat - V - to do C programming homework
If the word is not in the dictionary, ask the user if he/she wishes to add the word to the dictionary. If the user does want to add it to the dictionary, prompt for definition and whether the word is a verb or noun. If the user does not want to put the word in the dictionary do not prompt them for information, simply continue the program.
In C program how do i do this?
Well, first off, you'll need some way to store a bunch of those dictionary entry objects. For simplicity (and because it doesn't sound like this assignment is about hash tables), I'd suggest using a linked list. e.g.
struct DictionaryEntry {....};
struct DictionaryNode {
struct DictionaryEntry entry;
struct DictionaryNode *next;
};
struct Dictionary {
struct DictionaryNode *first;
}
(Note that my terminology is slightly different than yours - my Dictionary struct means the whole thing, whereas DictionaryEntry is a specific item).
Then write some functions to find words in the dictionary and to add new ones. If you were using C++ this would be slightly simpler, but it's good to know how to use malloc...
void addDictionaryEntry( struct Dictionary *dict, struct DictionaryEntry *entry ) {
struct DictionaryNode *node = malloc(sizeof(DictionaryNode));
memcpy( node-%26gt;entry.word, entry-%26gt;word, 10 );
memcpy( node-%26gt;entry.definition, entry-%26gt;word, 80 );
node-%26gt;entry.noun_or_verb = entry-%26gt;noun_or_verb;
node-%26gt;next = dict-%26gt;first;
dict-%26gt;first = node;
}
struct DictionaryEntry *getDictionaryEntryByWord( struct Dictionary *dict, char *word ) {
DictionaryNode *node = dict-%26gt;first;
while( node ) {
if( strcmp(node-%26gt;entry.word, word) == 0 ) {
return %26amp;node-%26gt;entry;
}
node = node-%26gt;next;
}
return null;
}
void initDictionary( struct Dictionary *dict ) {
dict-%26gt;first = 0;
}
// might want a dictionary destructor, too...
That should take care of the storage, which, this being C, is probably the trickiest part. Then you just have to have a decent main() and do the user I/O.
int main( int argc, char **argv ) {
struct Dictionary dict;
struct DictionaryEntry inputEntry;
struct DictionaryEntry *foundEntry;
initDictionary( %26amp;dict );
while( true ) {
printf("Enter a word%26gt; ");
scanf("%9s", inputEntry.word); // at most 9 chars, as we need one more for the string-terminating NUL
foundEntry = getDictionaryEntryByWord( %26amp;dict, inputEntry.word );
if( foundEntry ) {
// tell user about the word
} else {
// ask him if he wants to enter a new word, put the def
// in inputEntry, and addDictionaryEntry it.
}
}
}
I'll leave a few parts for you to fill in, as you probably already know how to make the UI code, but it is rather tedious to type in this text entry area. My code probably has a few syntax errors that'll need fixing, anyway ;)
Sunday, August 2, 2009
Can someone please check this code for me it is in C programing due today so please hurry?
#include %26lt;stdio.h%26gt;
#include %26lt;stdlib.h%26gt;
#include %26lt;time.h%26gt;
#include %26lt;conio.h%26gt;
#include %26lt;ctype.h%26gt;
int main (void)
{
//creating random balance for user
srand(time(NULL));
int ranBal;
ranBal = rand();
printf("\t\t Virtual Bank at Osceola\n");
printf("\t\t\t WELCOME\n\n");
char num1, num2, num3, num4;
printf("Please enter you 4 digit pin number: \n");
scanf("%c%c%c%c", %26amp;num1, %26amp;num2, %26amp;num3, %26amp;num4);
//Pin number code
for (int count = 0; count %26lt; 4; count++)
{
if (count == 3)
{
printf("Sorry You can't continue, contact your bank for assistance");
}
if ((isdigit(num1)) %26amp;%26amp; (isdigit(num2)) %26amp;%26amp; (isdigit(num3)) %26amp;%26amp;(isdigit(num4)))
{
printf("Valid Pin");
break;
}//end if
else
{
printf("Invalid Pin");
printf("Please try to enter your pin again");
scanf("%c%c%c%c", %26amp;num1, %26amp;num2, %26amp;num3, %26amp;num4);
}//end else
}//end for
int receipt;
printf("For Receipt enter 1, For no receipt enter 2\n");
scanf("%d", %26amp;receipt);
if (receipt == 1)
printf("Please take your receipt\n");
else if (receipt == 2)
printf("No receipt will be printed\n\n");
else
printf("Invalid Entry\n");
//clear screen
//clrscr();
int again = 1;
int option;
while (again == 1)
{
printf("Please choose one of the following numbers\n\n");
printf("1: Get Card Back 2: Fast Cash 3: Withdraw 4: Deposit 5: Balance\n\n");
scanf("%d", %26amp;option);
int fastCash;
if (option == 1)
{
printf("Goodbye!\n\n");
}//end option 1
printf("Press 1: Another Transaction or Press 2: Get Card Back");
scanf("%d", %26amp;again);
else if (option == 2)
{
printf("1: $20.00 2: $40.00 3: $80.00 4: $100.00\n\n");
scanf("%d", %26amp;fastCash);
if (fastCash == 1)
{
printf("You have withdrawn $20.00\n");
}
if (fastCash == 2)
{
printf("You have withdrawn $40.00\n");
}
if (fastCash == 3)
{
printf("You have withdrawn $80.00\n");
}
if (fastCash == 4)
{
printf("You have withdrawn $100.00\n");
}
printf("Press 1: Another Transaction or Press 2: Get Card Back");
scanf("%d", %26amp;again);
}//end option 2
else if (option == 3)
{
int withdrawl;
printf("Please enter withdrawl amount\n");
scanf("%d", withdrawl);
ranBal = ranBal - withdrawl;
if (ranBal %26lt; 0)
printf("");
//compare to actual balance which must be a randomized number
printf("Press 1: Another Transaction or Press 2: Get Card Back");
scanf("%d", %26amp;again);
}//end option 3
else if (option == 4)
{
printf("Please enter deposit amount\n");
printf("Press 1: Another Transaction or Press 2: Get Card Back");
scanf("%d", %26amp;again);
}//end option 4
else if (option == 5)
{
printf("Your balance is %d.\n", ranBal);
printf("Press 1: Another Transaction or Press 2: Get Card Back");
scanf("%d", %26amp;again);
//just print off randomized balance number
}//end option 5
else
{
printf("Invalid Entry\n");
printf("Press 1: Another Transaction or Press 2: Get Card Back");
scanf("%d", %26amp;again);
}
}
if (again == 2)
printf("Goodbye!");
if ((again != 1) %26amp;%26amp; (again != 2))
printf("Invalid Entry");
system("pause");
return (0);
}//end main
Can someone please check this code for me it is in C programing due today so please hurry?
I stopped reading after this line:
char num1, num2, num3, num4;
printf("Please enter you 4 digit pin number: \n");
scanf("%c%c%c%c", %26amp;num1, %26amp;num2, %26amp;num3, %26amp;num4);
which is so hopelessly messed up, I had to laugh for a few hours.
Anyway, there is no way to judge this program since you just threw code at us and not what it is supposed to do. So yeah, this code looks perfect, just like any other code someone might post.
Reply:That program is screwed up in more ways than a dozen. Good luck. You need to talk to your professor and tell her or him to help you. Your professor has obviously not communicating with you on how to write a program.
RJ
Reply:This must be for a university course.
It's hard to read because it's all crammed over to the left.
Review the use of indention, and ANSI standards.
Also, you have everything in the main function which also makes it hard to read, too much in one function.
Reply:Couple of things -
for (int count = 0; count %26lt; 4; count++) - what exactly is this for loop for? You should probably be using a switch statement.
Reply:Can you try with system("CLS") ; instead of clescr();
community survey
#include %26lt;stdlib.h%26gt;
#include %26lt;time.h%26gt;
#include %26lt;conio.h%26gt;
#include %26lt;ctype.h%26gt;
int main (void)
{
//creating random balance for user
srand(time(NULL));
int ranBal;
ranBal = rand();
printf("\t\t Virtual Bank at Osceola\n");
printf("\t\t\t WELCOME\n\n");
char num1, num2, num3, num4;
printf("Please enter you 4 digit pin number: \n");
scanf("%c%c%c%c", %26amp;num1, %26amp;num2, %26amp;num3, %26amp;num4);
//Pin number code
for (int count = 0; count %26lt; 4; count++)
{
if (count == 3)
{
printf("Sorry You can't continue, contact your bank for assistance");
}
if ((isdigit(num1)) %26amp;%26amp; (isdigit(num2)) %26amp;%26amp; (isdigit(num3)) %26amp;%26amp;(isdigit(num4)))
{
printf("Valid Pin");
break;
}//end if
else
{
printf("Invalid Pin");
printf("Please try to enter your pin again");
scanf("%c%c%c%c", %26amp;num1, %26amp;num2, %26amp;num3, %26amp;num4);
}//end else
}//end for
int receipt;
printf("For Receipt enter 1, For no receipt enter 2\n");
scanf("%d", %26amp;receipt);
if (receipt == 1)
printf("Please take your receipt\n");
else if (receipt == 2)
printf("No receipt will be printed\n\n");
else
printf("Invalid Entry\n");
//clear screen
//clrscr();
int again = 1;
int option;
while (again == 1)
{
printf("Please choose one of the following numbers\n\n");
printf("1: Get Card Back 2: Fast Cash 3: Withdraw 4: Deposit 5: Balance\n\n");
scanf("%d", %26amp;option);
int fastCash;
if (option == 1)
{
printf("Goodbye!\n\n");
}//end option 1
printf("Press 1: Another Transaction or Press 2: Get Card Back");
scanf("%d", %26amp;again);
else if (option == 2)
{
printf("1: $20.00 2: $40.00 3: $80.00 4: $100.00\n\n");
scanf("%d", %26amp;fastCash);
if (fastCash == 1)
{
printf("You have withdrawn $20.00\n");
}
if (fastCash == 2)
{
printf("You have withdrawn $40.00\n");
}
if (fastCash == 3)
{
printf("You have withdrawn $80.00\n");
}
if (fastCash == 4)
{
printf("You have withdrawn $100.00\n");
}
printf("Press 1: Another Transaction or Press 2: Get Card Back");
scanf("%d", %26amp;again);
}//end option 2
else if (option == 3)
{
int withdrawl;
printf("Please enter withdrawl amount\n");
scanf("%d", withdrawl);
ranBal = ranBal - withdrawl;
if (ranBal %26lt; 0)
printf("");
//compare to actual balance which must be a randomized number
printf("Press 1: Another Transaction or Press 2: Get Card Back");
scanf("%d", %26amp;again);
}//end option 3
else if (option == 4)
{
printf("Please enter deposit amount\n");
printf("Press 1: Another Transaction or Press 2: Get Card Back");
scanf("%d", %26amp;again);
}//end option 4
else if (option == 5)
{
printf("Your balance is %d.\n", ranBal);
printf("Press 1: Another Transaction or Press 2: Get Card Back");
scanf("%d", %26amp;again);
//just print off randomized balance number
}//end option 5
else
{
printf("Invalid Entry\n");
printf("Press 1: Another Transaction or Press 2: Get Card Back");
scanf("%d", %26amp;again);
}
}
if (again == 2)
printf("Goodbye!");
if ((again != 1) %26amp;%26amp; (again != 2))
printf("Invalid Entry");
system("pause");
return (0);
}//end main
Can someone please check this code for me it is in C programing due today so please hurry?
I stopped reading after this line:
char num1, num2, num3, num4;
printf("Please enter you 4 digit pin number: \n");
scanf("%c%c%c%c", %26amp;num1, %26amp;num2, %26amp;num3, %26amp;num4);
which is so hopelessly messed up, I had to laugh for a few hours.
Anyway, there is no way to judge this program since you just threw code at us and not what it is supposed to do. So yeah, this code looks perfect, just like any other code someone might post.
Reply:That program is screwed up in more ways than a dozen. Good luck. You need to talk to your professor and tell her or him to help you. Your professor has obviously not communicating with you on how to write a program.
RJ
Reply:This must be for a university course.
It's hard to read because it's all crammed over to the left.
Review the use of indention, and ANSI standards.
Also, you have everything in the main function which also makes it hard to read, too much in one function.
Reply:Couple of things -
for (int count = 0; count %26lt; 4; count++) - what exactly is this for loop for? You should probably be using a switch statement.
Reply:Can you try with system("CLS") ; instead of clescr();
community survey
Java printf problem, System.out.printf("%*c", n, ' '); doesn't work?
When I try to format my text by adding n width
i.e System.out.printf("%*c", n, ' ');
with n being the number of spaces, ' ' being the blank space char.
I get the error:
Exception in thread "main" ava.util.UnknownFormatConversionExceptio... Conversion = '*'
Java printf problem, System.out.printf("%*c", n, ' '); doesn't work?
there is no in build method like "printf" in Java. so if you Reilly wants to use it , then you have to write definition for the
printf() method explicitly.
Reply:The problem is printf. This is not a java method.
You should be using System.out.print() or System.out.println().
See the link below for the tutorial on how to use formatting.
i.e System.out.printf("%*c", n, ' ');
with n being the number of spaces, ' ' being the blank space char.
I get the error:
Exception in thread "main" ava.util.UnknownFormatConversionExceptio... Conversion = '*'
Java printf problem, System.out.printf("%*c", n, ' '); doesn't work?
there is no in build method like "printf" in Java. so if you Reilly wants to use it , then you have to write definition for the
printf() method explicitly.
Reply:The problem is printf. This is not a java method.
You should be using System.out.print() or System.out.println().
See the link below for the tutorial on how to use formatting.
Using c++, how do you read a file character by character?
I'm using Dev C++ to create a program. I am familiar with fopen(), fscanf() and fprintf(). Using fscanf() i can get values, strings and such, but not white spaces. How can i get the file char by char, including spaces, tabs and new lines?
Using c++, how do you read a file character by character?
fgetc()
Reply:getchar()
Reply:ok, you need to use your fsreams to get the job done.
Ex:
#include %26lt;iostream%26gt;
#include %26lt;fstream%26gt;
#include %26lt;string%26gt;
using namespace std;
int main()
{
string fileline; //the string where the text from the file is being stored
ofstream File ("NewFile.txt"); //opens the file to be read
File.close();
//*opens and close NewFile.txt, we'll be putting our code in between these functions
//*This code will read the file and place it on the screen
//*There is no need to edit this code
ifstream SecondFile ("NewFile.txt");
while(! SecondFile.eof() ) //this while loop gets the text from the file line by line
{
getline(SecondFile, fileline); //get the line and put it in the fileline
cout %26lt;%26lt; fileline %26lt;%26lt; endl; //write the fileline onto the screen
}
SecondFile.close(); //ALWAYS CLOSE THE FILE
system("PAUSE");
}
Reply:Look into cin.getline(), cin.getchar(). Those will allow you to get a line until you hit a specific character (usually the \n, but you can specify which character) and put it into a char variable. It will also grab white space.
Reply:Since you are talking about the c libraries (cstdio in C++, stdio.h in C use the functions you mentioned) you can use fgetc, getc, or fread. You will find them discussed in the Dev-C++documentation and I'll link to the pages on cplusplus.com -but my keyboard is acting up and I'm tired so I don't DARE try to offer an example.
I'd go with fread to minimize the chance of skipping over whitespace characters.
Reply:fgetc()
Look into cin.getline(), cin.getchar(). Those will allow you to get a line until you hit a specific character (usually the \n, but you can specify which character) and put it into a char variable. It will also grab white space.
getchar()
Using c++, how do you read a file character by character?
fgetc()
Reply:getchar()
Reply:ok, you need to use your fsreams to get the job done.
Ex:
#include %26lt;iostream%26gt;
#include %26lt;fstream%26gt;
#include %26lt;string%26gt;
using namespace std;
int main()
{
string fileline; //the string where the text from the file is being stored
ofstream File ("NewFile.txt"); //opens the file to be read
File.close();
//*opens and close NewFile.txt, we'll be putting our code in between these functions
//*This code will read the file and place it on the screen
//*There is no need to edit this code
ifstream SecondFile ("NewFile.txt");
while(! SecondFile.eof() ) //this while loop gets the text from the file line by line
{
getline(SecondFile, fileline); //get the line and put it in the fileline
cout %26lt;%26lt; fileline %26lt;%26lt; endl; //write the fileline onto the screen
}
SecondFile.close(); //ALWAYS CLOSE THE FILE
system("PAUSE");
}
Reply:Look into cin.getline(), cin.getchar(). Those will allow you to get a line until you hit a specific character (usually the \n, but you can specify which character) and put it into a char variable. It will also grab white space.
Reply:Since you are talking about the c libraries (cstdio in C++, stdio.h in C use the functions you mentioned) you can use fgetc, getc, or fread. You will find them discussed in the Dev-C++documentation and I'll link to the pages on cplusplus.com -but my keyboard is acting up and I'm tired so I don't DARE try to offer an example.
I'd go with fread to minimize the chance of skipping over whitespace characters.
Reply:fgetc()
Look into cin.getline(), cin.getchar(). Those will allow you to get a line until you hit a specific character (usually the \n, but you can specify which character) and put it into a char variable. It will also grab white space.
getchar()
Remove values in a string in c++ programming?
how do i remove a reoccurrence of a value in a string:
this is how i started it
string message = " classic class"
char r = 's"
i now want to remove all the char remove in string message using a void function
void remove(string message, char r)
string::size_type pos, stop;
pos = message.find(r, 0);
stop = message.length();
while (pos %26lt; stop){
message.erase (pos,stop);
pos = message.find (c, pos + 1);
}
cout %26lt;%26lt; message%26lt;%26lt; endl;
somehow i cannot get an output, displaying .......claic cla
can someone please help me
Remove values in a string in c++ programming?
i think that the mistake was in this line
message.erase(pos,stop) in the while loop
coz when you did this pos=message.find(r,0);
pos locates the first 's' in the string
and stop=message.length;
so when u wrote this //message.erase(pos,stop);
it removes all the characters starting from the first 's'
u located with (pos) to the end of the string (stop)
and the string then becomes "cla"
u can do this
string message="classic class";
char r='s';
while (message.find(r)!=string::npos) /*message.find(r) returns string::npos if it doesn't find the char 's' and when this happens it breaks the loop */
{
string::size_type pos=message.find(r);
message.erase(pos,1); /* 1 in the second parameter to erase only one character which must be an 's' */
}
cout%26lt;%26lt;message%26lt;%26lt;endl;
/* pure code to use :D */
string message="classic class";
char r='s';
while (message.find(r)!=string::npos)
{
string::size_type pos=message.find(r);
message.erase(pos,1);
}
cout%26lt;%26lt;message%26lt;%26lt;endl;
hope i helped u and sorry for my bad english :$
Reply:I haven't done C++ in a while, but I did notice a few things:
1. "message.erase (pos,stop);"
Seems like you're removing everything from the first "s" to the length of the message.
2. "pos = message.find (c, pos + 1);"
'c' is not declared? This doesn't seem good if the letter is removed, since all the letters are renumbered. That is assuming the erase function does what you say.
3. "while (pos %26lt; stop){"
Perhaps you should test the condition if nothing is found in the 'pos' variable?
this is how i started it
string message = " classic class"
char r = 's"
i now want to remove all the char remove in string message using a void function
void remove(string message, char r)
string::size_type pos, stop;
pos = message.find(r, 0);
stop = message.length();
while (pos %26lt; stop){
message.erase (pos,stop);
pos = message.find (c, pos + 1);
}
cout %26lt;%26lt; message%26lt;%26lt; endl;
somehow i cannot get an output, displaying .......claic cla
can someone please help me
Remove values in a string in c++ programming?
i think that the mistake was in this line
message.erase(pos,stop) in the while loop
coz when you did this pos=message.find(r,0);
pos locates the first 's' in the string
and stop=message.length;
so when u wrote this //message.erase(pos,stop);
it removes all the characters starting from the first 's'
u located with (pos) to the end of the string (stop)
and the string then becomes "cla"
u can do this
string message="classic class";
char r='s';
while (message.find(r)!=string::npos) /*message.find(r) returns string::npos if it doesn't find the char 's' and when this happens it breaks the loop */
{
string::size_type pos=message.find(r);
message.erase(pos,1); /* 1 in the second parameter to erase only one character which must be an 's' */
}
cout%26lt;%26lt;message%26lt;%26lt;endl;
/* pure code to use :D */
string message="classic class";
char r='s';
while (message.find(r)!=string::npos)
{
string::size_type pos=message.find(r);
message.erase(pos,1);
}
cout%26lt;%26lt;message%26lt;%26lt;endl;
hope i helped u and sorry for my bad english :$
Reply:I haven't done C++ in a while, but I did notice a few things:
1. "message.erase (pos,stop);"
Seems like you're removing everything from the first "s" to the length of the message.
2. "pos = message.find (c, pos + 1);"
'c' is not declared? This doesn't seem good if the letter is removed, since all the letters are renumbered. That is assuming the erase function does what you say.
3. "while (pos %26lt; stop){"
Perhaps you should test the condition if nothing is found in the 'pos' variable?
What could I do to copy a string into a multiple array string in c++?
I am writing a program in c++, I would like to copy a srting into a multiple array string. i have used the srtcpy() function but Iam getting an error message telling me that :
-invalid conversion from 'char' to 'char*'
-initializing argument 1 of 'char*' strcpy(char*, const char*).
//this is what I get as message. and here is my program:
# include%26lt;iostream%26gt;
#include%26lt;string%26gt;
using namespace std;
int main()
{int namsize;
char names[20][20], newname[18] ;
cout%26lt;%26lt;"what is the size of your name";
cin%26gt;%26gt;namsize;
cout%26lt;%26lt;" please enter the name";
cin.getline(newname,namsize);
for (int i=0;i%26lt;=(namsize+1);i++)
strcpy(names[i][1],newname[i]);
system("pause");
return 0;
}
please could you help me to solve this problem? thanks
What could I do to copy a string into a multiple array string in c++?
do strcpy(names[i],newname) instead of
strcpy(names[i][1],newname[i])
and the for loop should go from 0 to 19.
for (int i=0;i%26lt;20;i++)
strcpy(names[i],newname);
( i am assuming you want to create 20 copies of the same name in the array)
strcpy has the loop built into it. you just need to pass the pointer to the char array to it ,terminated by null of course.(If u din get the last part, its ok)
Reply:this is a syntax error! you need spaces between char names [20] [20]..that should work!
Reply:/*
just do copy of one string onto the next
eg. shown below is a c++ code
*/
# include%26lt;iostream%26gt;
#include%26lt;string%26gt;
using namespace std;
int main()
{
int namsize;
string names="fanda", newname ;
newname=names;
cout %26lt;%26lt;"\nnames: "%26lt;%26lt;names;
cout%26lt;%26lt;"\nnewname: "%26lt;%26lt;newname;
cout%26lt;%26lt;endl;
system("pause");
return 0;
}
Reply:You are using C++. Seriously consider using std::vector and std::string. Then you don't have to ask really odd questions like "what is the size of your name".
Also consider joining a YahooGroup for C/C++ programming instead of using Yahoo Answers for questions like this. You'll learn a LOT more that way by being part of a community who shares the same interests and goals.
order flowers
-invalid conversion from 'char' to 'char*'
-initializing argument 1 of 'char*' strcpy(char*, const char*).
//this is what I get as message. and here is my program:
# include%26lt;iostream%26gt;
#include%26lt;string%26gt;
using namespace std;
int main()
{int namsize;
char names[20][20], newname[18] ;
cout%26lt;%26lt;"what is the size of your name";
cin%26gt;%26gt;namsize;
cout%26lt;%26lt;" please enter the name";
cin.getline(newname,namsize);
for (int i=0;i%26lt;=(namsize+1);i++)
strcpy(names[i][1],newname[i]);
system("pause");
return 0;
}
please could you help me to solve this problem? thanks
What could I do to copy a string into a multiple array string in c++?
do strcpy(names[i],newname) instead of
strcpy(names[i][1],newname[i])
and the for loop should go from 0 to 19.
for (int i=0;i%26lt;20;i++)
strcpy(names[i],newname);
( i am assuming you want to create 20 copies of the same name in the array)
strcpy has the loop built into it. you just need to pass the pointer to the char array to it ,terminated by null of course.(If u din get the last part, its ok)
Reply:this is a syntax error! you need spaces between char names [20] [20]..that should work!
Reply:/*
just do copy of one string onto the next
eg. shown below is a c++ code
*/
# include%26lt;iostream%26gt;
#include%26lt;string%26gt;
using namespace std;
int main()
{
int namsize;
string names="fanda", newname ;
newname=names;
cout %26lt;%26lt;"\nnames: "%26lt;%26lt;names;
cout%26lt;%26lt;"\nnewname: "%26lt;%26lt;newname;
cout%26lt;%26lt;endl;
system("pause");
return 0;
}
Reply:You are using C++. Seriously consider using std::vector and std::string. Then you don't have to ask really odd questions like "what is the size of your name".
Also consider joining a YahooGroup for C/C++ programming instead of using Yahoo Answers for questions like this. You'll learn a LOT more that way by being part of a community who shares the same interests and goals.
order flowers
C++, using default file name?
I'm writing a code for matrix multiplication. At one point the program asks the user to type in the file name to get the file data. If the user presses 'Enter,' I need the program to use a default file name. I can't get this part to work right. If the user inputs a file name, it works fine, but if they press enter it won't use the default name. (I'm using Visual C++ 2005 btw.) Here's the part of the code that addresses my problem:
int const size = 80;
int i, j;
char cstring[size];
char DEFAULTINPUTFILE = "input.txt";
cout %26lt;%26lt; "Please type the input file's name:";
cin %26gt;%26gt; cstring;
ifstream inputFile;
inputFile.append(cstring);
if ( cstring[0] == '\0' )
cstring = DEFAULTINPUTFILE;
inputFile.open(cstring);
Any advice (I want to understand how to do this) would be much appreciated. Thanks in advance!
C++, using default file name?
try
cstring.length() == 0;
int const size = 80;
int i, j;
char cstring[size];
char DEFAULTINPUTFILE = "input.txt";
cout %26lt;%26lt; "Please type the input file's name:";
cin %26gt;%26gt; cstring;
ifstream inputFile;
inputFile.append(cstring);
if ( cstring[0] == '\0' )
cstring = DEFAULTINPUTFILE;
inputFile.open(cstring);
Any advice (I want to understand how to do this) would be much appreciated. Thanks in advance!
C++, using default file name?
try
cstring.length() == 0;
Subscribe to:
Posts (Atom)