Ok, the thing is, I have no idea how to do it legitimately with strings, cin etc., so I did it in twisted way.. the problem is I have to enter the word twice, I have no idea why.
For example, I enter Name, press enter, then I have to enter the same or different name again and it will automatically press enter after there are the same amount of characters as in the first name.. so I would try to enter Cupcakes for the second time, but after fourth character, c, the loop will be stopped and it will give me cpuC as a result.
The thing is I don't want to type second time. If it's impossible to change that I would, at least, like program to say something like "Now enter the name second time, for fun". Thanks.
1 2 3 4 5 6 7 8 9 10
#include <iostream>
#include <conio.h>
usingnamespace std;
int main()
{
char j[1000];
int n=0;
while (cin.get() != '\n'){j[n]= _getch(); cout << j[n]; n++;}
while (n>=0) {cout << j[n]; n--;}
}
I have no idea what you mean. Do you mean something like
1 2
int t=0;
while (t<=300) {j[n]=t; t++;}
?
If I understand correctly I think I should initialize j[n] with as much as elements as the word length is going to be.. but how should I know that? My loop does it for me by asking to type the word twice.
I mean that in the second loop there is no such initializrd element of the array for the initial value of n. Moreover it is a bad idea to use non-standard function _getch.
How come there isn't? n has value after the first loop (because value rises from zero). As far as I understand, If n value is for example y (which is the same number as there are characters in a word), then in the second loop initial j[n] value will be y too, which will go down to zero.
Well consider the first loop. Let assume that you entered only one element (character). In this case the following were done 1) j[0] was assigned a value; 2) n become equal to 1.
So what value does j[1] have that is used in the second loop?
As I said your program is invalid. You should at first write the program correctly without _getch and with correct access to elements of the array and only after that ask other questions.
The point of this program was to reverse the given word without using strings or something like that. There is no way to change this program to correct, the only one option is to write another program from the beginning which I can't do because I don't know how, that's why I created this invalid program in the first place
#include <iostream>
int main()
{
constint N = 1000;
char s[N];
int i = 0;
char c;
while ( i < N && ( c = std::cin.get() ) != '\n' )
{
s[i++] = c;
}
while ( i != 0 ) std::cout << s[--i];
std::cout << std::endl;
}