I've been searching these forums and trying to figure this out on my own but am having problems.
int x;
cin >> x;
if a noninteger value is entered the program goes into an endless loop - is there anything that can be done about this? :confused:
thanks - if you can help elucidate this i would appreciate it as i'm trying to get a firm grasp on c++ and this is throwin off my game
6 2581
so it looks like it loops because of a condition i had set - but if it is passed a noninteger value it just ignores every condition and falls through the code till it returns
any suggestions?
Banfa 9,065
Expert Mod 8TB
Not unless you post the code that is causing the problem
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
/*This program takes two int, one coefficient, and one exponent to calculate the
power of the coefficient - it stores each multiplication in a vector and displays
the progression from 1 through...*/
using namespace std;
//function declaration
int powervec(int coef, int expo, vector<int> &chamber);
int main(int argc, char *argv[])
{
int x = 0, y = 0;
char s; //input character for y/n response
int cnt = 1; //counter for exponential count
int check = 1;//control variable to control loop through process
while(check == 1)//repeat loop while response 'y'
do
{
cnt = 1; //reset counter to 1
cout << "\nEnter two values - One Coefficient & One Exponent\n\n\n";
cout << "Enter Coefficient: ";
while(!(cin >> x))
{cout << "\n!!!invalid input received!!!\n\nTry again...\n\n"; break;}
cout << "\nEnter Exponent: ";
while(!(cin >> y))
{cout << "\n!!!invalid input received!!!\n\nTry again...\n\n"; break;}
cout << endl;
if(y == 1 && x == 1)
{cout << "\n1 multiplied by 1 is 1\n\n";
cout << "do you want to try again?(y or n):"; cin >> s;
if(s == 'y'){break;}
else {check = 0; break;}
}
vector<int> powvector(y);
powervec(x,y,powvector);
for(vector<int>::iterator iter = powvector.begin(); iter != powvector.end(); iter++, cnt++)
{
cout << x << " to the power of " << cnt << " is: " << *iter << endl;
}
cout << "\nCalculate another power??? (y or n):";
cin >> s;
if(s == 'n'){check = 0;} //escape loop
}while (s == 'y' || s == 'Y');
cout << "\nThank you for calculating powers with my program!!!\n" << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
int powervec(int coef, int expo, vector<int> &chamber)
{
int total = 1;
for(int num = 0; num != expo; num++)
{
total *= coef;
chamber[num] = total;
}
return total;
}
------------------------------------------------------------------------
Again, when ever you enter a noninteger value into x or y, the program loops endlessly.
Banfa 9,065
Expert Mod 8TB
My Comments embedded in the code in bold -
// MethodTest.cpp : Defines the entry point for the console application.
-
//
-
-
#include <cstdlib>
-
#include <iostream>
-
#include <string>
-
#include <vector>
-
-
/*This program takes two int, one coefficient, and one exponent to calculate the
-
power of the coefficient - it stores each multiplication in a vector and displays
-
the progression from 1 through...*/
-
-
using namespace std;
-
-
//function declaration
-
-
int powervec(int coef, int expo, vector<int> &chamber);
- /* Since this is in the same module as the place it is
-
called from you could prefix this with static */
-
-
int main(int argc, char *argv[])
-
{
-
int x = 0, y = 0;
-
-
char s; //input character for y/n response
-
int cnt = 1; //counter for exponential count
-
int check = 1;//control variable to control loop through process
- /* You seem to be using check as a boolean control variable so why
-
not use the typer bool? Also check is not very descriptive of what
-
this variable does a name like bContinueRunning might be better */
-
-
while(check == 1)//repeat loop while response 'y'
- /* This loop is a bit unnecessary, you could just add the
-
condition to the conditions of the do loop like so
-
-
}while ((s == 'y' || s == 'Y') && check == 1);
-
-
Although you would also need to change some of your
-
breaks to continues. */
-
-
do
-
{
-
-
cnt = 1; //reset counter to 1
-
-
cout << "\nEnter two values - One Coefficient & One Exponent\n\n\n";
-
cout << "Enter Coefficient: ";
-
-
while(!(cin >> x))
-
{cout << "\n!!!invalid input received!!!\n\nTry again...\n\n"; break;}
- /* This line is part of your problem, you detect the error and output a
-
message (cout statement) but instead of re-requesting input you
-
then break out of the loop this code is equivilent to
-
-
if(!(cin >> x))
-
{
-
cout << "\n!!!invalid input received!!!\n\nTry again...\n\n";
-
}
-
-
this means that if the input value is invalid the program just goes ahead and tries to use it.
-
*/
-
- /* Your source format is very poor and makes the code less readable,
-
which in turn makes it harder to maintain the 2 normal ways of laying
-
out the braces of a loop or if statement would be
-
-
while(!(cin >> x)) {
-
cout << "\n!!!invalid input received!!!\n\nTry again...\n\n";
-
break;
-
}
-
-
or
-
-
while(!(cin >> x))
-
{
-
cout << "\n!!!invalid input received!!!\n\nTry again...\n\n";
-
break;
-
}
-
-
personally I prefer the second. Formated like this you might have
-
spotted the break error.
-
*/
-
cout << "\nEnter Exponent: ";
-
while(!(cin >> y))
-
{cout << "\n!!!invalid input received!!!\n\nTry again...\n\n"; break;}
- /* Again the same break problem and source code formating.*/
-
cout << endl;
-
if(y == 1 && x == 1)
-
{cout << "\n1 multiplied by 1 is 1\n\n";
- /* Source code formating. */
-
cout << "do you want to try again?(y or n):"; cin >> s;
-
if(s == 'y'){break;}
- /* Source code formating and here you only check for y but later
-
on you check for y and Y, you should keep these checks consistent.
-
Additionally you are actually repeating the code to ask if the user
-
wants to perform another calculation. This is bad form as it introduces
-
a maintence issue (if you want to change the method you have to
-
change it in 2 places). By reorganising the if with an else clause
-
you can have the code that checks for a repeat calculation once at
-
the end of the loop removing this issue. This would also allow you to
-
remove the outer while loop entirely and the variable check. */
-
else {check = 0; break;}
- /* Source code formating. */
-
}
-
vector<int> powvector(y);
-
-
powervec(x,y,powvector);
-
-
for(vector<int>::iterator iter = powvector.begin(); iter != powvector.end(); iter++, cnt++)
-
{
-
cout << x << " to the power of " << cnt << " is: " << *iter << endl;
-
}
-
-
cout << "\nCalculate another power??? (y or n):";
-
-
cin >> s;
-
-
if(s == 'n'){check = 0;} //escape loop
- /* Source code formating and below you check for y and Y, but here
-
you ar checking for n you should keep your checks */
-
-
}while (s == 'y' || s == 'Y');
-
-
cout << "\nThank you for calculating powers with my program!!!\n" << endl;
-
-
system("PAUSE");
-
return EXIT_SUCCESS;
-
-
}
-
-
int powervec(int coef, int expo, vector<int> &chamber)
- /* Since this function clearly does not cope with negative exponents
-
it might be better to have expo as an unsigned or at least
-
check that it is >= 0 */
-
{
-
int total = 1;
-
-
for(int num = 0; num != expo; num++)
-
{
-
total *= coef;
-
chamber[num] = total;
-
}
-
-
return total;
-
-
}
-
Banfa 9,065
Expert Mod 8TB
After a bit of research I find that rather than -
int x;
-
-
while(!(cin >> x))
-
{
-
cout << "\n!!!invalid input received!!!\n\nTry again...\n\n";
-
}
-
you may find this more reliable -
char input[100];
-
int x;
-
char *pEnd;
-
-
do
-
{
-
cin.getline( input, sizeof input );
-
-
x = (int)strtol(input, &pEnd, 10);
-
-
if (!x)
-
cout << "\n!!!invalid input received!!!\n\nTry again...\n\n";
-
}
-
while(!x);
-
Oi Bantfa!
That rocks...thanks a lot, bro :)
My programming style has totally been impacted in a very positive way
I'm more proficient with selective execution
The way I go about planning is more practical
dude, thank you thank you thank you
just what i needed to here :D
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Berislav Lopac |
last post by:
Imagine three options:
a) There is an XML file. A PHP5 script loads it and puts it into a DOM
document object. Then, given an input string, a function looks via XPath
for a node with that name...
|
by: claudel |
last post by:
Hi
I have a newb PHP/Javascript question regarding checkbox processing
I'm not sure which area it falls into so I crossposted to comp.lang.php
and comp.lang.javascript.
I'm trying to...
|
by: Hari |
last post by:
Basically I would like to downlod the visual basic 6.0 compiler, but i
already have the vb.net compiler. I had to pay for the VB.net IDE, just
wondering if I can get the vb 6.0 IDE for free or not....
|
by: David E. |
last post by:
So as a programmer, what's the best thing to study? EJB? How much of the
J2EE or Enterprise architecture is necessary to no? I guess I need a good
overview for a newb like me... thanks..
--...
|
by: Alexandre |
last post by:
Hi,
Im a newb to dev and python... my first sefl assigned mission was to read a
pickled file containing a list with DB like data and convert this to
MySQL... So i wrote my first module which...
|
by: Walter |
last post by:
But I'm stumped.....
I've got a windows 2000 server and I am trying to set up PHPBB on it using a
mysql database.. I am very inexperienced on this.....
Ive installed mysql V4.0.20d and I can...
|
by: claudel |
last post by:
Hi
I have a newb PHP/Javascript question regarding checkbox processing
I'm not sure which area it falls into so I crossposted to comp.lang.php
and comp.lang.javascript.
I'm trying to...
|
by: notbob |
last post by:
Newb here! Using 4.0.20 on Slack. Slogging through the official
manual. At 2.4.3 Securing the Initial MySQL Accounts, I'm finally
stopped cold while trying to follow instructions. Here's what I...
|
by: segue |
last post by:
Just Curious.
|
by: Anthony J. Biondo Jr. |
last post by:
Hi Everyone:
I was wondering if it is common practice to use some sort of messaging
(MSMQ, WebSphere MQ) with web services. I understand the need for messaging
for insert / update transactions,...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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,...
|
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...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |