Getting loop code to output.

Hey so I am new to C++ and have been trying to write a piece of code that would sudo encrypt some text, basically I want to convert it to ascii then increase it by its position in the alphabet. That comes next for now I am trying to get a for loop to output code so it can be used in the main function. If anyone knows where I am going wrong that would be great.

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
#include "stdafx.h"
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int x;

int convertToASCII(string letter)
{
	for (int i = 0; i < letter.length(); i++)
	{
		char x = letter.at(i);
		x = i + i; //this is just an example of a way to change the ascii each iteration through the loop, though any suggestions would be nice.
		return int(x);
	}
}

int main()
{
	cout << "please enter text" << '\n';
	   
	string input = "";
	getline(cin, input);
	convertToASCII (input);
	cout << x << '\n'; // This is just for testing before I continue writing code.

	cin.clear();
	cin.ignore(255, '\n');
	cin.get();

    return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>

using namespace std;

const int x = 1;                          // increment by this 

void convertToASCII(string &letter)       // don't return int; needs to change letter, so precede with &
{
	for (int i = 0; i < letter.length(); i++) 
	{
		letter[i] += x;            // some encoding
	}
}

int main()
{
   string input;
   cout << "Please enter text:" << '\n';
   getline( cin, input );
   convertToASCII( input );
   cout << input << endl;
   return 0;
}
Wow, that is a lot easier that I thought it would be, thank you.
I do have another question, is there a way to have a user defined integer for x? I have tried to use the code provided but when I run the code it does not output anything. Lastly I would like some advice on having the text output to a file created by the program.

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
41
#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int a;

int x = a;                       

void Encrypt(string &letter)     
{
	for (int i = 0; i < letter.length(); i++)
	{
		letter[i] += x;
	}
}

int main()
{
	cout << "please enter a number (you must give the person you send the message to the number)" << '\n';

	cin >> a;

	string input;

	cout << "Please enter text:" << '\n';

	getline(cin, input);

	Encrypt(input);

	cout << input << '\n';

	cin.clear();                 //just here to stop the window from closing during testing.
	cin.ignore(255, '\n');
	cin.get();

	return 0;
}
Avoid using global variables if possible - especially those that change. Your method wouldn't work anyway, because you try to set x equal to a before a is set.

The code below will prompt the user for x and send it to your encrypting routine as an argument.

Other comments:
- If you mix cin >> and getline(), then note that cin >> will take its data, but won't necessarily pass the EOL character (\n), so the next getline() call will just take the rest of the "line", which will effectively be blank. I have circumvented this by another cin.ignore() call, but it is probably better to use getline() for any input and stringstream the resulting string into your variables (see the tutorials on this site).

- I think you could usefully learn about reading and writing files; see:
http://www.cplusplus.com/doc/tutorial/files/
For a standard text file, you basically have to:
- open a filestream with ofstream;
- write to it in exactly the same way as you would to the terminal, replacing 'cout' by the name of your stream;
- when you have finished, just close the filestream.
This is just three lines of code (in this program) and should be straightforward if you look at the tutorial.


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
#include <iostream>
#include <string>
#include <fstream>

using namespace std;


void Encrypt(string &letter, int x)     
{
	for (int i = 0; i < letter.length(); i++)
	{
		letter[i] += x;
	}
}

int main()
{
        int x;
	string input;

	cout << "please enter a number (you must give the person you send the message to the number)" << '\n';
	cin >> x;     cin.ignore(255, '\n');

	cout << "Please enter text:" << '\n';
	getline(cin, input);

	Encrypt(input, x);
	cout << input << '\n';

	cin.get();     // effectively pauses
        return 0;
}

Thanks again, I shall avoid global variables where possible. And put a little more thought into the code structure.

I was able to get the code to output into a text file I created but not create one through running the code, it is not an issue but I am curious would making the program through the console 32 in visual studio 2015 make any difference to the ability to create a file on my HDD?
I can't really advise you on Visual Studio. I run from the command line in Windows or a standard bash shell in Linux.

It should certainly be able to create a hard-disk text file in VS, but where in the file system I have no idea.
Fair enough, I will stick to VS for now until I stop making so many mistakes haha. I will have to do a little more research into it, again thanks for you help.
Topic archived. No new replies allowed.