473,388 Members | 1,234 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,388 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 1313
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.