473,327 Members | 2,094 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Please tell me what I'm doing wrong

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
6 1312
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
xeys_00 wrote:
...


Works fine ...

--
Gregor Razdrtic [Mufe]
mu**@poizen.org
Jul 23 '05 #3
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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

303
by: mike420 | last post by:
In the context of LATEX, some Pythonista asked what the big successes of Lisp were. I think there were at least three *big* successes. a. orbitz.com web site uses Lisp for algorithms, etc. b....
6
by: What-a-Tool | last post by:
I'm going out out of my mind trying to get this to work with no luck. The error message I get is at the bottom. Can someone please tell me what I'm doing wrong here. I've tried this a million...
39
by: Scotter | last post by:
Okay I think my title line was worded misleadingly. So here goes again. I've got quite 20 identical MDB files running on an IIS5 server. From time to time I need to go into various tables and add...
5
by: TrvlOrm | last post by:
HI There, I have been struggling with JavaScript code for days now, and this is my last resort! Please help... I am trying to create a JavaScript slide show with links for Next Slide,...
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
22
by: rasiel | last post by:
I'm hoping someone can help me out. I'm a researcher in need of developing an automated database and would like to see if someone here is willing to consider putting together for me a simple...
17
by: Amy | last post by:
Hi, I finished this script and for some reason there is a delay every so often in the timing. Sometimes it seems two take 2 seconds instead of 1. Can anyone see anything that would slow it down? I...
9
by: FERHAT AÇICI | last post by:
hi all! who know arrays on visual basic please tell me.... thanks..
22
by: Amali | last post by:
I'm newdie in c programming. this is my first project in programming. I have to write a program for a airline reservation. this is what i have done yet. but when it runs it shows the number of...
112
by: Prisoner at War | last post by:
Friends, your opinions and advice, please: I have a very simple JavaScript image-swap which works on my end but when uploaded to my host at http://buildit.sitesell.com/sunnyside.html does not...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.