By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,191 Members | 787 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,191 IT Pros & Developers. It's quick & easy.

Please tell me what I'm doing wrong

P: n/a
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);
}

Jul 23 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
xeys_00 wrote:
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.
Jul 23 '05 #2

P: n/a
xeys_00 wrote:
...


Works fine ...

--
Gregor Razdrtic [Mufe]
mu**@poizen.org
Jul 23 '05 #3

P: n/a
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.

Jul 23 '05 #4

P: n/a
"xeys_00" writes:
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();
}

Jul 23 '05 #5

P: n/a
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

Jul 23 '05 #6

P: n/a
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

Jul 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.