learning by writing my own program

ok, this should be the last question for this program I am trying to write.

I am getting the error:

1
2

Error	C2440	'initializing': cannot convert from 'std::basic_istream<char,std::char_traits<char>>' to 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>'	


when I try to compile..

here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <fstream>
#include <string>
#include <cstdio>
#include <algorithm>
#include <istream>


using namespace std;
	

int main()
{

	// creating a string variable to hold a word
	

	// Ask user to input a word to be translated to "PIG_LATIN"
	cout << ("Enter a word to be translated to PIG_LATIN:\n");
	std::string word = cin >> word;
	word = word = 'a';


	int lengthOFword = word.length();

// subtract 1 from lengthOFword so that the 'a' remains at the end
	lengthOFword = lengthOFword - 1;

// move first letter the distance of lengthOFword and places it before the 'a'
	std::swap(word[1], word[lengthOFword]);

//print the final product
	cout << (word);


	

	std::getchar();
}


Thank you in advance for any, and all, assistance.

"how do you get to Carnegie hall? PRACTICE PRACTICE PRACTICE
Last edited on
hello Mikey41m,

Your error come from line 20. You should just define std::string word;on its own line and cin >> word; on its own line. That should fix you error, but it does not fix the rest of the problems like with the swap.

Hope thathelps,

Andy

P.S. Do a Google search on pig latin or pig latin c++
Last edited on
@Mikey41m

In pig latin, you don't swap the first letter with anything. You move it to the end of the word, then add "ay". Like if you used the word "goat", it would convert to "oatgay".

I fixed up your program a bit, and it now works, as intended.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <fstream>
#include <string>
#include <cstdio>
#include <algorithm>
#include <istream>


using namespace std;


int main()
{

	// creating a string variable to hold a word
	string word = "";

	// Ask user to input a word to be translated to "PIG_LATIN"
	cout << ("Enter a word to be translated to PIG_LATIN:\n");
	cin >> word;
	word += "ay"; // Add these two letters to end of word
	
	int lengthOFword = word.length();

	// subtract 1 from lengthOFword so that the 'a' remains at the end
	lengthOFword-=3; // Return key + 'a' + 'y'. 3 spaces

	// move first letter the distance of lengthOFword and places it before the 'a'
	char move_letter = word[0]; // Use 0, NOT 1
	//std::swap(word[0], word[lengthOFword]); // Don't use swap
	for (int x = 0; x < lengthOFword; x++) // Moves letters to the left, then replace
		word[x] = word[x + 1];                   //word[lengthOFword] with first letter
		
	word[lengthOFword] = move_letter;
	//print the final product
	cout << (word) << endl << endl;

	std::getchar();
}
thank you Andy, I will make that change right away. Would you be so kind as to list the other problems and help me with how to fix them? This is my first attempt to write my own code without copying someone else's work. I knew I would make mistakes but that is how you learn, (trial and error).
Thank you again Andy. I am still impressed with myself for getting as far as I did with the code. It is nice to know I was somewhat close considering I wrote it from mind. I googled a few things here and there and attempted to add them in when I found something close to what I was looking for (i.e. the swap statement (not everything I found that looked right was but I was still on the right track)). I always thought for pig latin you just put the first letter at the end and added an 'a' to it. Thank you for correcting me on that as well. I will make the appropriate changes and consider this project somewhat of a success even with fatal flaws I mistakingly added in.
Andy, something did not work right on trying to run this code.

1
2
3
4
5
6
7

Enter a word to be translated to PIG_LATIN:
jar
rajay

Press any key to continue .  .  .


for some reason it switched the "R" from the end of the word to the beginning. Other then that, it works as it should. I am looking through the code to see if I can figure it out. If you have any suggestions, please chime in. Thank you so much Andy... You have helped a lot.
Last edited on
on line 32:

1
2
3

word[x] = word[x + 2];


I changed 2 to 1 and it worked right

1
2
3

word[x] = word[x + 1]



next problem is, when I build it and run the exe, the console window does not stay open. I figured either the endl or the std::getchar(); would have fixed that.
Last edited on
@Mikey41m

I'm not sure where you got line 32 as word[x] = word[x + 2] as I wrote it with a + 1. Anyway, here's slightly different ending, so the console stays open, with a cin >> word;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
include <iostream>
#include <fstream>
#include <string>
#include <cstdio>
#include <algorithm>
#include <istream>


using namespace std;


int main()
{

	// creating a string variable to hold a word
	string word = "";

	// Ask user to input a word to be translated to "PIG_LATIN"
	cout << ("Enter a word to be translated to PIG_LATIN:\n");
	cin >> word;
	word += "ay"; // Add these two letters to end of word
	
	int lengthOFword = word.length();

	// subtract 1 from lengthOFword so that the 'a' remains at the end
	lengthOFword-=3; // Return key + 'a' + 'y'. 3 spaces

	// move first letter the distance of lengthOFword and places it before the 'a'
	char move_letter = word[0];
	//std::swap(word[0], word[lengthOFword]); // Don't swap
	for (int x = 0; x < lengthOFword; x++) // Move letters, then replace
		word[x] = word[x + 1];                   //word[lengthOFword] with first letter
		
	word[lengthOFword] = move_letter;
	//print the final product
	cout << (word) << endl << endl;

	cin >> word; // To stop console from closing
}
Last edited on
sorry Andy, I misread that line I guess... I really want to thank you for your help. You think cin >> word will do the trick? That simple eh? just take out std::getchar(); and replace it with cin >> word... ok I will make that change... thank you again Andy. Sorry I mis read what you had on that one line the first time around.
@Mikey41m

My name is whitenite1, not Andy
again I am sorry... Andy posted something and so I automatically thought it was the same person replying. I deeply apologize whitenite1. You have been a major help.
No problem. I've done the same thing.
Oh, before somebody mentions it, you shouldn't double post.
http://www.cplusplus.com/forum/beginner/197885/

If the problem is still with the main thread you've started, stick with it. Only use a different thread, when you are requesting help on a totally different set of problems, with a different program, etc.

Happy programming!!
that is what I did..

the first time I had a problem I was getting this error

1
2
3
4

 
  '{': missing function header (old-style formal list?)


from this, the code I first wrote

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>


using namespace std;
	

int main();
{

// creating a string variable to hold a word
	string word;

// Ask user to input a word to be translated to "PIG_LATIN"
cout ("Enter a word to be translated to PIG_LATIN:\n");
cin >> word;

int lengthOFword = (word.length)
return 0
}


after that was answered, I wrote the rest of the code that is listed above and then made this post. I was trying to not ask for to much help and I was trying to not clutter to much.

I have the exe file but now I am working on creating a setup file so that I can pass this program to a couple friends for fun. Damn visual studios 2015 is twisting me in circles with trying to figure it out tho hehe... I have read through several walk throughs with no luck.
it is 9:30 and I am getting old so I think I will head to bed and try to figure the setup file tomorrow. Thank you a ton for the help whitenite1. You have been a lifesaver. I am still impressed that I got as close as I did with how little knowledge I have of c++ maybe next time I will get even closer to my next project. Good night and sweet dreams to all.
Topic archived. No new replies allowed.