|
 |
|
 |
 |
04-24-2005, 08:12 AM
|
#1 (permalink)
|
|
Registered User
Join Date: Apr 2005
Posts: 9
|
need help with copying backwards c++
ok i am a newbie here. I am just learning this material . please be nice. i
i am trying to make a program spell backwards. almost a wordgame anyways the user will enter a word up to 20 characters and then the program will type it backwords. i thought i had this perfect but it comes up with a header function error. this is my code. Can anyone give me any pointers on how to fix this?( i have more code then this and the rest of it works fine ) but this option i cant seem to get it to work.
Thank you
~VAl
void WordGame1(void)
{
char str_frontwards[20];
char str_backwards[20];
while (str_frontwards[20] != '\0')
}
/*coping a string to spell backwards*/
{
char str_frontwards[20];
char str_backwards[20];
strcpy (str_backwards[0],str_frontwards[20]);
strcpy (str_backwards[1],str_frontwards[19]);
strcpy (str_backwards[2],str_frontwards[18);
strcpy (str_backwards[3],str_frontwards[17]);
strcpy (str_backwards[4],str_frontwards[16]);
strcpy (str_backwards[5],str_frontwards[15]);
strcpy (str_backwards[6],str_frontwards[14]);
strcpy (str_backwards[7],str_frontwards[13]);
strcpy (str_backwards[8],str_frontwards[12]);
strcpy (str_backwards[9],str_frontwards[11]);
strcpy (str_backwards[10],str_frontwards[10]);
strcpy (str_backwards[11],str_frontwards[9]);
strcpy (str_backwards[12],str_frontwards[8]);
strcpy (str_backwards[13],str_frontwards[7]);
strcpy (str_backwards[14],str_frontwards[6]);
strcpy (str_backwards[15],str_frontwards[5]);
strcpy (str_backwards[16],str_frontwards[4]);
strcpy (str_backwards[17],str_frontwards[3]);
strcpy (str_backwards[18],str_frontwards[2]);
strcpy (str_backwards[19],str_frontwards[1]);
strcpy (str_backwards[20],str_frontwards[0]);
printf ("str_backwards[20]");
printf ("str_backwards[19]");
printf ("str_backwards[18]");
printf ("str_backwards[17]");
printf ("str_backwards[16]");
printf ("str_backwards[15]");
printf ("str_backwards[14]");
printf ("str_backwards[13]");
printf ("str_backwards[12]");
printf ("str_backwards[10]");
printf ("str_backwards[9]");
printf ("str_backwards[8]");
printf ("str_backwards[7]");
printf ("str_backwards[6]");
printf ("str_backwards[5]");
printf ("str_backwards[4]");
printf ("str_backwards[3]");
printf ("str_backwards[2]");
printf ("str_backwards[1]");
printf ("str_backwards[0]");
}
__________________
Last edited by rogue : 04-24-2005 at 02:51 PM.
Reason: c== in title
|
|
|
04-24-2005, 09:28 AM
|
#2 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,680
|
First of all strcpy() copies a string, not a char, what you do for your readin routine I have no idear of, since all it seems to be is a while loop that does absolutely nothing.
Something like this will do the trick:
Code:
#include <stdio.h>
#include <string.h>
#define LINE_SIZE 20
int main()
{
char correct_word[LINE_SIZE +1], reverse_word[LINE_SIZE+1];
int i, j;
printf("Please enter word to be reversed: ");
fflush(stdout);
if(!fgets(correct_word, LINE_SIZE, stdin))
{
printf("Error reading word from stdin\n");
return -1;
}
/* correct for teh '\n' and '\0' from the read string */
j = strlen(correct_word) -2;
for(i = 0; j >= 0; )
reverse_word[i++] = correct_word[j--];
/* make sure reversed word isn't ending in garbadge */
reverse_word[i] = '\0';
printf("Read word was: %s", correct_word);
printf("Reversed word: %s\n", reverse_word);
return 0;
}
|
|
|
04-24-2005, 09:56 AM
|
#3 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,680
|
Here is one in C++, altho I think Valmont can come up with a more pretty solution.
Code:
#include <iostream>
#include <string>
const int LINE_SIZE = 20;
using namespace std;
int main()
{
string correct_word;
char temp_word[LINE_SIZE+1];
int i, j;
cout << "Please enter word to be reversed: ";
cout.flush();
cin >> correct_word;
/* correct for the '\0' from the read string */
j = correct_word.size() -1;
for(i = 0; j >= 0; )
temp_word[i++] = correct_word[j--];
/* make sure reversed word isn't ending in garbadge */
temp_word[i] = '\0';
string reverse_word(temp_word);
cout << "Read word was: " << correct_word << endl;
cout << "Reversed word: " << reverse_word << endl;
return 0;
}
|
|
|
04-24-2005, 11:06 AM
|
#4 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,680
|
I couldn't help but take a sneeky peek at your deleted question..
From what I can see you have a real problem placing your code, into useful functions which will allow you to use them throughout your program.
The %s within the argument to printf() will tell it that there will be a string substitution uppon runtime, this is just one of the conversion chars which functions like [s|f]printf() and [s|f]scanf() among others takes.
I don't have the time right now, but within the next 5 or 6 hours I will correct the code you provided there, into a program which will do what you want it to do, with comments explaining how everything is ment to work together.
|
|
|
04-24-2005, 12:00 PM
|
#5 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
|
The power of STL for my entertainment:
Code:
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
string TheWord("I am a string!");
reverse(TheWord.begin(), TheWord.end());
cout<<TheWord<<endl;
return 0;
}
Code:
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
string TheWord("I am a string!");
string Reversed(TheWord); //sizing
reverse_copy(TheWord.begin(), TheWord.end(), Reversed.begin());
cout<<Reversed<<endl;
return 0;
}
Many variations to this code can be made. And all of them require an IQ of any one-cell organism 
__________________
|
|
|
04-24-2005, 01:40 PM
|
#6 (permalink)
|
|
Registered User
Join Date: Apr 2005
Posts: 9
|
valmont quick question
What i'm trying to do is make a wordgame to accept a wpord upto 20 letters. and then the program will spell it backwards.
This is the code i am working with.i need to get case3 to go to my word game 1. now instead of a print f(display) i need to make a function to open wordgame1. i am a little lost on how to do this. i think i am on the right track but not sure. I just want to know am i close to getting the function to work ?
Yes or no.
i think the statement i need is soemthing like this :
DisplayWordGame1();
fflush(stdin);
/*program to wordgame1*/
/* AUTHOR: Valerie */
/* DATE: 4/20/05 */
Code:
/* DisplayMenu. */
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
void DisplayMenu(void);
void WordGame1(void);
int main()
{
char UserResponse;
DisplayMenu();
fflush(stdin);
UserResponse = getch();
while (UserResponse != '1'&& UserResponse !='2'&& UserResponse != '3'
&&UserResponse != '4'&& UserResponse !='5')
{
fflush(stdin);
printf("%c", 7);
UserResponse = getch();
}
while (UserResponse != '5')
{
switch(UserResponse)
{
case '1': printf("\n\tYou have selected 1");
break;
case '2': printf("\n\tYou have selected 2");
break;
case '3':
break;
case '4': printf("\n\tYou have selected 4");
break;
}
printf("\n\n\n\tHit any key to continue....");
fflush(stdin);
getch();
}
DisplayMenu();
fflush(stdin);
UserResponse = getch();
while (UserResponse != '1'&& UserResponse !='2'&& UserResponse != '3'
&&UserResponse != '4'&& UserResponse !='5')
{
fflush(stdin);
printf("%c", 7);
UserResponse = getch();
}
printf("\n\nGoodbye\n\n\n");
return 0;
}
void DisplayMenu(void)
{
system("cls");
printf("\n\t1.Sequential update");
printf("\n\t2.Control break");
printf("\n\t3.Word game 1");
printf("\n\t4.Word Game2");
printf("\n\t5.Quit");
}
void WordGame1(void)
#include <stdio.h>
#include <string.h>
#define LINE_SIZE 20
{
char Frontwards[LINE_SIZE +1], Backwards[LINE_SIZE+1];
int i, j;
printf("Please enter word up to 20 letters: ");
fflush(stdout);
if(!fgets(Frontwards, LINE_SIZE, stdin))
{
printf("Error reading word from stdin\n");
}
/* correct for the '\n' and '\0' from the read string */
j = strlen (Frontwards) -2;
for(i = 0; j >= 0; )
Backwards[++i] = Frontwards[--j];
/* make sure reversed word isn't ending in garbage */
Backwards[i] = '\0';
printf("Read word was: %s", Frontwards);
printf("Backwards: %s\n", Backwards);
{
printf("\n\n\n\tHit any key to continue....");
fflush(stdin);
getch();
}
}
__________________
Last edited by Valmont : 04-24-2005 at 03:03 PM.
|
|
|
04-24-2005, 04:10 PM
|
#7 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,680
|
Alright, here is my version of this program..
What it is supposed to do with the otehr 4 selections in the menu, I'm not that involved with.
Code:
/* A good description of the program should always be at the beginning
*
* Program name: Wordgame
* Author: Valerie
* Date: April 24. 2005
* Usage: Wordgame
* The rest is delth with by
* questions asked to the user
*
* Description: This program will ask the user to provide
* it with a word, this word will be repeated
* and followed by the word spelled backwards.
* It will not be allowed to provide the program
* with more than one word at a time.
* Whould the input given, contain more than one
* word, it is _only_ the first word that will be
* repeated and spelled backwards.
*
* Return value: The following return values will be given
* 0 -> success
* -1 -> Error getting input from user
* -2 -> Error trying to reverse word from user
*/
#include <stdio.h> /* provides access to fgets()/printf()/fflush() */
#include <string.h> /* provides access to strlen() */
#include <stdlib.h> /* provides access to atoi() */
/* Since we like to make this program only
* read words of upto 20 chars in length,
* It is essential that we keep that limit
* strict
*/
#define WORD_LENGTH 20
/* I never belive in functions returning void
* It's better to let them return an int
* especialy in this case, the display_menu will
* return the selection the user makes as an integer.
* when 0 is returned the user selected exit/quit
*/
int display_menu(void);
/* It is required to hava a function, which gets the word
* needed in order to spell it backwards.
* This will also check for any error in the user input
* so there will only be _one_ word to spell backwards.
* The word provided by the user will be in the argument
* given to the function, after a call to this function.
*/
int get_word(char* word);
/* Since this is the wordgame program, which is intended to
* spell the provided word backwards, we need to have a function
* that knows how to reverse a word.
* The first argument is the word intended for reversing,
* the second argument will be holding the reversed word after
* a call to this function.
*/
int reverse_word(char* orrig_word, char* rev_word);
/* This is where it is all combined, give a clean run with as much
* responsebility spread out across all the different function,
* which makes the main function as clean and slinder as possible.
*/
int main(void)
{
char normal_word[WORD_LENGTH +1], reversed_word[WORD_LENGTH +1];
int user_choice;
while(0 < (user_choice = display_menu()))
{
switch(user_choice)
{
case 1:
/* what ever the program should do when user selects 1 */
break;
case 2:
/* what ever the program should do when user selects 2 */
break;
case 3:
/* Our world famous word reverse program weee... */
printf("\nWelcome the the wordgame of reversing words\n");
printf("Your word to be reversed: ");
fflush(stdout);
if(get_word(normal_word))
{
printf("Error reading the word\n");
return -1;
}
if(reverse_word(normal_word, reversed_word))
{
printf("Error reversing word\n");
return -2;
}
printf("Provided word: %s\n", normal_word);
printf("Reversed word: %s\n", reversed_word);
break;
case 4:
/* what ever the program should do when user selects 4 */
break;
case 5:
/* what ever the program should do when user selects 5 */
break;
default:
/* Some unknown input, user_choise is holding it */
printf("Sorry, the provided choice [%d] is unknown.\n", user_choice);
break;
}
/* let the user know, we're about to continue */
printf("Press enter to continue.\n");
/* make sure nothing is held up in the out stream */
fflush(stdout);
/* fetch the users "enter" for continued running */
fgets(normal_word, WORD_LENGTH, stdin);
/* make sure there's no garbadge in normal_word, for future read */
normal_word[0] = '\0';
}
/* Should there have been an error, let the program reflect that */
return user_choice;
}
/* This is the user interaction, making sure the user only inputs
* valid selection, adn cleaning up the stdin/stdout stream for
* any following requests.
* the function will basicaly provide the user with a menu, and
* return the value given from the user, shoudl that be a number
* a char or anything else, if the user gives q/Q it will return 0
* indicating a successful return, thus basis for program termination
*/
int display_menu(void)
{
char choice[WORD_LENGTH +1];
/* make the user aware of what choices this program provides */
printf("\t\t.---------------------.\n");
printf("\t\t| Welcome to Wordgame |\n");
printf("\t\t|---------------------|\n");
printf("\t\t| 1) Sequential update|\n");
printf("\t\t| 2) Control break |\n");
printf("\t\t| 3) Word Game 1 |\n");
printf("\t\t| 4) Word Game 2 |\n");
printf("\t\t| 5) Some action |\n");
printf("\t\t| q) QUIT |\n");
printf("\t\t'---------------------'\n");
printf("\t\t Your choice: ");
fflush(stdout);
if(!fgets(choice, WORD_LENGTH, stdin))
{
printf("Error getting selection from user\n");
return -1;
}
if(choice[0] == 'q' || choice[0] == 'Q')
return 0;
return atoi(choice);
}
/* Here the read of the user provided word will be set,
* anything not conforming with somethign that whoudl be
* in a word, will make it stop readign the word provided.
*/
int get_word(char* word)
{
char buffer[WORD_LENGTH +1];
int i = 0;
if(!fgets(buffer, WORD_LENGTH, stdin))
return -1;
fflush(stdin);
/* make sure only the very first word gets returned */
for( ; buffer[i] &&
buffer[i] != '\t' &&
buffer[i] != ' ' &&
buffer[i] != '\r' &&
buffer[i] != '\n'; ++i)
word[i] = buffer[i];
word[i] = '\0';
return 0;
}
/* Reverse the word given.
*/
int reverse_word(char* orrig_word, char* rev_word)
{
/* since get_word() strips it for '\n' we correct
* the length for the '\0' which will be there
*/
int i = 0, j = strlen(orrig_word) -1;
if(j <= 0)
return -1;
for( ; j >= 0; )
rev_word[i++] = orrig_word[j--];
/* make sure reversed word isn't ending in garbadge */
rev_word[i] = '\0';
return 0;
}
Sorry, but at this hour of teh night, I'm not upto coloring every preserved word in the program and such.. So you will have to do with only the comments beeing colored.
|
|
|
04-24-2005, 04:13 PM
|
#8 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
|
Actually it looks pretty neat like that. Here are a few colors though.
Code:
/* A good description of the program should always be at the beginning
*
* Program name: Wordgame
* Author: Valerie
* Date: April 24. 2005
* Usage: Wordgame
* The rest is delth with by
* questions asked to the user
*
* Description: This program will ask the user to provide
* it with a word, this word will be repeated
* and followed by the word spelled backwards.
* It will not be allowed to provide the program
* with more than one word at a time.
* Whould the input given, contain more than one
* word, it is _only_ the first word that will be
* repeated and spelled backwards.
*
* Return value: The following return values will be given
* 0 -> success
* -1 -> Error getting input from user
* -2 -> Error trying to reverse word from user
*/
#include <stdio.h> /* provides access to fgets()/printf()/fflush() */
#include <string.h> /* provides access to strlen() */
#include <stdlib.h> /* provides access to atoi() */
/* Since we like to make this program only
* read words of upto 20 chars in length,
* It is essential that we keep that limit
* strict
*/
#define WORD_LENGTH 20
/* I never belive in functions returning void
* It's better to let them return an int
* especialy in this case, the display_menu will
* return the selection the user makes as an integer.
* when 0 is returned the user selected exit/quit
*/
int display_menu(void);
/* It is required to hava a function, which gets the word
* needed in order to spell it backwards.
* This will also check for any error in the user input
* so there will only be _one_ word to spell backwards.
* The word provided by the user will be in the argument
* given to the function, after a call to this function.
*/
int get_word(char* word);
/* Since this is the wordgame program, which is intended to
* spell the provided word backwards, we need to have a function
* that knows how to reverse a word.
* The first argument is the word intended for reversing,
* the second argument will be holding the reversed word after
* a call to this function.
*/
int reverse_word(char* orrig_word, char* rev_word);
/* This is where it is all combined, give a clean run with as much
* responsebility spread out across all the different function,
* which makes the main function as clean and slinder as possible.
*/
int main(void)
{
char normal_word[WORD_LENGTH +1], reversed_word[WORD_LENGTH +1];
int user_choice;
while(0 < (user_choice = display_menu()))
{
switch(user_choice)
{
case 1:
/* what ever the program should do when user selects 1 */
break;
case 2:
/* what ever the program should do when user selects 2 */
break;
case 3:
/* Our world famous word reverse program weee... */
printf("\nWelcome the the wordgame of reversing words\n");
printf("Your word to be reversed: ");
fflush(stdout);
if(get_word(normal_word))
{
printf("Error reading the word\n");
return -1;
}
if(reverse_word(normal_word, reversed_word))
{
printf("Error reversing word\n");
return -2;
}
printf("Provided word: %s\n", normal_word);
printf("Reversed word: %s\n", reversed_word);
break;
case 4:
/* what ever the program should do when user selects 4 */
break;
case 5:
/* what ever the program should do when user selects 5 */
break;
default:
/* Some unknown input, user_choise is holding it */
printf("Sorry, the provided choice [%d] is unknown.\n", user_choice);
break;
}
/* let the user know, we're about to continue */
printf("Press enter to continue.\n");
/* make sure nothing is held up in the out stream */
fflush(stdout);
/* fetch the users "enter" for continued running */
fgets(normal_word, WORD_LENGTH, stdin);
/* make sure there's no garbadge in normal_word, for future read */
normal_word[0] = '\0';
}
/* Should there have been an error, let the program reflect that */
return user_choice;
}
/* This is the user interaction, making sure the user only inputs
* valid selection, adn cleaning up the stdin/stdout stream for
* any following requests.
* the function will basicaly provide the user with a menu, and
* return the value given from the user, shoudl that be a number
* a char or anything else, if the user gives q/Q it will return 0
* indicating a successful return, thus basis for program termination
*/
int display_menu(void)
{
char choice[WORD_LENGTH +1];
/* make the user aware of what choices this program provides */
printf("\t\t.---------------------.\n");
printf("\t\t| Welcome to Wordgame |\n");
printf("\t\t|---------------------|\n");
printf("\t\t| 1) Sequential update|\n");
printf("\t\t| 2) Control break |\n");
printf("\t\t| 3) Word Game 1 |\n");
printf("\t\t| 4) Word Game 2 |\n");
printf("\t\t| 5) Some action |\n");
printf("\t\t| q) QUIT |\n");
printf("\t\t'---------------------'\n");
printf("\t\t Your choice: ");
fflush(stdout);
if(!fgets(choice, WORD_LENGTH, stdin))
{
printf("Error getting selection from user\n");
return -1;
}
if(choice[0] == 'q' || choice[0] == 'Q')
return 0;
return atoi(choice);
}
/* Here the read of the user provided word will be set,
* anything not conforming with somethign that whoudl be
* in a word, will make it stop readign the word provided.
*/
int get_word(char* word)
{
char buffer[WORD_LENGTH +1];
int i = 0;
if(!fgets(buffer, WORD_LENGTH, stdin))
return -1;
fflush(stdin);
/* make sure only the very first word gets returned */
for( ; buffer[i] &&
buffer[i] != '\t' &&
buffer[i] != ' ' &&
buffer[i] != '\r' &&
buffer[i] != '\n'; ++i)
word[i] = buffer[i];
word[i] = '\0';
return 0;
}
/* Reverse the word given.
*/
int reverse_word(char* orrig_word, char* rev_word)
{
/* since get_word() strips it for '\n' we correct
* the length for the '\0' which will be there
*/
int i = 0, j = strlen(orrig_word) -1;
if(j <= 0)
return -1;
for( ; j >= 0; )
rev_word[i++] = orrig_word[j--];
/* make sure reversed word isn't ending in garbadge */
rev_word[i] = '\0';
return 0;
}
__________________
|
|
|
04-24-2005, 04:22 PM
|
#9 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,680
|
If you have any questions, I'll answer them tomorrow, once I get to work or sometime during the day.. Anyway, since you say your professor isn't explaining everything in a way you can understand, then feel free to come here and ask for further elaboration to what he might have ment with the things he is teaching you.
Just one thing tho, next time please dont just delete your posts, it makes it quite hard to keep up with the conversation in the thread. Other users will have a very difficult time reading up on what have happened here, they are not like Valmont and myself, who are capable of reading everything since we're moderators on this board.
Edit: Thanks for the coloring Valmont, I havn't had time to create a coloring program yet.. But once there are two thursdays in a week I might get around to it.
|
|
|
04-24-2005, 04:39 PM
|
#10 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,680
|
Quote:
|
Originally Posted by rouge
i think the statement i need is soemthing like this :
DisplayWordGame1();
fflush(stdin);
|
Sorry I didn't see this when I was readign your post, I guess I was more focusing on what you code was doing..
You can't flush stdin with fflush(), it only works on output strams, since stdin is an inuput stream you will need to clear it another way, somethign like this
Code:
int clear_stream(FILE* stream)
{
int input;
while(input = fgetc(stream))
{
if(input == (int)'\n')
{ /* we're at the end of stream */
return 0;
}
/* its garbadge clugging up stream */;
}
/* some error happened reading from stream */
return -1;
}
but be warned the above code hasn't been tested, it was just somethign I came up with.
I'm too tired now, I have to get some sleep, it's almost 3am here, and I need to get up in another 3 hours.. See you all tomorrow.
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 02:17 AM.
|
Copyright © 2000-2006, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
Open Circle
|
 |
|