New To C++

Well as the title says, I'm pretty new to C++. Earlier I felt like I was actually starting to understand it so I decided to go ahead and try to make a rather simple program.

The ideas of the program was to allow the user to input letters for their name one at a time. And if they entered in a valid one, then it would allow them to put in the next and the next and so on. If you wanted to terminate the program you would be able to type in end. At the end of this all once you type in finish, it would out put all the letters you put together. I had this idea and thought it would be pretty cool if I could do it. I've made some alternatives since my other plans hadn't worked. But I'd like to know your guys' ideas on this.

So far this is what I got:

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

using namespace std;

int main ()
{
	char a, b, c;
	int x;
	
	cout << "Hello, please enter in the first letter of your name then follow by hitting enter and placing the next letter." << endl;
	cin >> a;
	cout << "Did you put " << a << "?" << endl;
	cout << "Hit 1 for yes, hit two for no" << endl;
	cin >> x;
	
	if (x == 1)
		cout << "Please put in next number" << endl;

	else if (x == 2)
		cout << "Returning to beginning" << endl;

}


Its not much because I have no idea where to go from there. Or am I just doing it all wrong? Help anyone?
Last edited on
to return to the beginning you need to use a while loop
and you cant input the next number without the cin >>
the problem is wat if i choose no for did u put __ . what i think u should do is to define what the program ahould be doing. I think its call psuedo code or sumthing.(search on wikipedia)
Is this what u mean? Try this, working or not:
#include <iostream>
#include<string>
#include<stdio>

using namespace std;

int main ()
{
char a[2],s[100]="Name is ";
int i, x;

cout << "Hello, please enter in the first letter of your name then follow by hitting enter and placing the next letter." << endl;
start:
for(i=0;;)
{
again:
cout<<"\nPress 'x' to end.";
gets(a);
if(!strcmp(a,"x")) goto last;
else
{
cout << "Did you put " << a << "?" << endl;
cout << "Hit 1 for yes, hit 2 for no" << endl;
cin >> x;
if (x == 1)
{
strcat(s,a);
cout << "Please put in next number" << endl;
goto start;
}
if (x == 2)
{
cout << "Returning to beginning" << endl;
goto again;
}
}
}
last:
puts(s);
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string> //<-- much easier then using chr(s)

using namespace std;

int main ()
{
     string name;
     int x;
     while(x != 1) { //loop in case they made a mistake
          cout << "Enter your name: ";
          cin.getline(name,256); //get the first 256 characters they input
          cout << endl << "Your name is " << name;
          cout << "Enter 1 for yes, anything else for no.";
          cin >> x;
          if(x == 1) {
               //do more stuff
          }
     }
}


Or you could simplify it, and do it this way...if you don't understand anything, you can first looking for information on that part, and then asking questions if you don't find anything.
Topic archived. No new replies allowed.