last thing I need help on with this code.

I have read many times that to end a program and keep the console window open you have to add in, std::getchar(); ,and I have that in there on the last line. Works perfectly if I run it from visual studio 2015 but when I build the exe and run it from the exe it flashes the result and closes before you can read the result. Ok, enough of the explaination of my problem and now for the code. I wrote this program today, it is my very first program that was not copied. I have had help in a couple spots on different threads but the jist of it was from me. I am trying to write programs to learn c++ from.

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
	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";


	int lengthOFword = word.length();

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

// move first letter the distance of lengthOFword and places it before the 'a'
	char move_letter = word[0];
	for (int x = 0; x < lengthOFword; x++)
		word[x] = word[x + 1];

	word[lengthOFword] = move_letter;

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

	std::getchar();
}
closed account (iGLbpfjN)
Just add cin.ignore(); before getchar(); and it should work
Last edited on
As Omni said, you will need to add cin.ignore() and getchar(); because Visual Studio does not keep the window open for you.
Topic archived. No new replies allowed.