Please tell me what I'm doing wrong

X

xeys_00

I'm trying to use the character "q" to exit this program. But if
anything but q is input , I want the loop to continue. Am I using the
wrong loop?

#include <string>
#include <iostream>

std::string first_name;
std::string last_name;
std::string full_name;
int i=0;
char quit='q';
int main()
{


first_name = "John";
last_name = "Doe";
full_name = first_name + " " + last_name;
std::cout << "Full name is " << full_name << "\n";
do {
std::cout << "What element of array do you want to see?";
std::cin >> i;
std::cout << "Value of i is " << full_name.at(i) << "\n";
std::cout << "Press q to quit. \n";
std::cin >> quit;
} while (quit!='q');
return (0);
}
 
E

E. Robert Tisdale

xeys_00 said:
I'm trying to use the character "q" to exit this program.
But if anything but q is input , I want the loop to continue.
Am I using the wrong loop?
cat main.cc
#include <string>
#include <iostream>

int main(int argc, char* argv[]) {
std::string first_name = "John";
std::string last_name = "Doe";
std::string full_name = first_name + " " + last_name;
std::cout << "Full name is " << full_name << ".\n";
size_t i = 0;
char quit = 'q';
do {
std::cout << "What element of array do you want to see? ";
std::cin >> i;
std::cout << "full_name.at(i) = "
<< full_name.at(i) << std::endl;
std::cout << "Press q to quit. \n";
std::cin >> quit;
} while (quit != 'q');
return 0;
}
g++ -Wall -ansi -pedantic -o main main.cc
./main
Full name is John Doe.
What element of array do you want to see? 2
full_name.at(i) = h
Press q to quit.
c
What element of array do you want to see? 3
full_name.at(i) = n
Press q to quit.
q

It seems to work just fine for me.
 
X

xeys_00

Well, I'm a beginner. The main function you used is pretty complex for
me. Is there just a easy answer? I'd like help based on not going
further than the existing complexity of the code(as easy as that seems
to be for everyone but me). I am new. I will learn, but it will take a
while.
 
O

osmium

xeys_00 said:
I'm trying to use the character "q" to exit this program. But if
anything but q is input , I want the loop to continue. Am I using the
wrong loop?
<snip>

The choice of a do-while loop is good. Your program seems to work OK as
long as the user follows the rules. If he types a letter when your program
expects a number you can have big problems. You can spend an inordinate
amouint of time trying to make even a simple thing such as this foolproof.

You violate one rule of good design, you introduce global variables, the
ones declared before main(). Here is a cut at a somewhat better version but
it is still not bulletproof. If the user enters two letters instead of one
when asked for a 'q", an upper case 'Q' thinking he can quit, enters much
too large a number when asked for a number, or enters a negative number, it
will still have problems.

----------------------
#include <iostream>
#include <string>


using namespace std;

int get_int()
{
int n;
do
{
cin >> n;
if(cin.fail())
{
cin.clear(); // clear the fail state
cin.ignore(1000, '\n'); // empty the buffer
cout << "Digits only\n";
}
else
return n;
}while(1);
}
//=======================
int main()
{
string fn = "John Smith";
int last_ix = fn.length() - 1;
char quit;
do
{
cout << "Which character?\n";
int n = get_int();
while(n > last_ix)
{
cout << "maximum index is " << last_ix << endl;
n = get_int();
}
cout << "Character selected is " << fn[n] << endl;
cout << "Press 'q' to quit\n";
cin >> quit;
} while(quit != 'q');
cin.get();
}
 
S

Samee Zahur

I might have a clue here... if you're expecting that simply pressing
'q' would let the user get out, you might be a little disappointed at
this: standard console input methods need the user to press ENTER or
RETURN before any of the input is processed. Your code should allow the
user to quit, if s/he presses ENTER after pressing 'q'

If you want this thing to go by a single keypress, you'll need to use
non-standard (and often non-portable) functions like getch() in header
<cconio>

Samee
 
X

xeys_00

I just wanted to say thanks for the feedback and help. I'm learning on
my own in prep for another semester at school. I heard a rumor they are
going to java; I hope not, as I'm working on C++ in the hopes I don't
appear too foolish in the class. I have decided this will all be more
interesting if I take problems and ideas that I have and apply
programming to them. I posted a number guessing prog yesterday, and
even though it's a "baby" program, I feel that if I am doing something
of interest to me it will retain my interest more. I'd eventually like
to make the jump to text based games. But I'm trying to crawl before I
walk. Thank much once again, and have a good day.

Xeys
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top