|
Harold_Wishes wrote: I might mention that the code below does not test for non-numeric values...
True. If that is a requirement, then I would use getchar() instead.
"The largest fire starts but with the smallest spark." - David Crow
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
You are doing far too much work for this.
int getInput(int range_min, int range_max)
{
int ret = range_min - 1;
do
{
char buffer[100] = {0};
cout << "Enter a value: ";
cin.getline(buffer, 99, '\n');
ret = atoi(buffer);
if (range_min > ret || ret > range_max)
{
cout << "Invalid entry!" << endl;
}
} while (range_min > ret || ret > range_max);
return ret;
}
int main()
{
int count = getInput(1, 11);
cout << "You entered: " << count << endl;
}
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
Harold, what I would do is:
<br />
char buff[1024];<br />
unsigned int value;<br />
int charsRead;<br />
<br />
bool validEntry = false;<br />
while (!validEntry){ <br />
cout << "Please enter the number of subunits: ";<br />
cin.getline(buff, 1023, '\n');<br />
<br />
if (sscanf(buff, "%u%n",&value,&charsRead)==1 && value >=1 && value <= 12)<br />
validEntry = true;<br />
}<br />
Otherwise, if you want to use c++ style stuff (I rarely do because I mostly work with windows programs and text strings), you could do sth like this:
<br />
int test;<br />
std::string dump;<br />
<br />
bool valid = false;<br />
while(!valid) {<br />
cout << "Please enter a number: ";<br />
cin >> test;<br />
<br />
if (!cin.fail())<br />
valid = true;<br />
else {<br />
cin.clear();<br />
cin >> dump;
}<br />
<br />
if (valid && (test < 1 || test > 12)) {<br />
valid = false;<br />
cout << "Out of range" << endl;<br />
}<br />
<br />
}<br />
cout << "You entered " << test << endl;<br />
PS: how does everyone get code to not remove all spacing?
earl
-- modified at 14:53 Wednesday 12th July, 2006
|
|
|
|
|
I think this code below compiles. I only reformatted. I shortened the logic in the program as suggested and it works. The only problem I'm having is duplicate output (something I encountered last week). Below the user is prompted to enter data twice in sequence. The first time the user is prompted to make a selection from a menu screen (no big deal).
But, after the user hits the ENTER key, the user will see duplicates of the second prompt. It is as if the user hit the enter key twice.
How do I prevent this from happening? Better question . . . Why is it happening?
I know the compiler is taking me thru the while loop an extra time which is printing the duplicate prompt (confirmed by the debugger).
#include <cstring>
#include <string>
#include <iostream>
using namespace std;
int main()
{
int data2;
char buff[1024];
unsigned int value;
int charsRead;
char selection;
cout << "A) Complete B) Partial C) UNKNOWN" << endl;
cin >> selection;
bool validEntry = false;
while (!validEntry)
{
cout << "Please enter the number of subunits: ";
cin.getline(buff, 1023, '\n');
if (sscanf(buff, "%u%n",&value,&charsRead)==1 && value >=1 && value <= 12)
validEntry = true;
}
data2 = value;
return 0;
}
-- modified at 21:56 Wednesday 12th July, 2006
|
|
|
|
|
I include pthread.h but the compiler produced an error which was ( fatal error C1083: Cannot open include file: 'pthread.h': No such file or directory) why was this error produced?
|
|
|
|
|
pthread is a header you wrote ?
i doubt it exists on visual C++ compiler.
aren't you compiling a source you found on the internet with a wrong compiler ?
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
|
|
|
|
|
toxcct wrote: pthread is a header you wrote
No it's Posix thread library ported to Win32 i think by Cygnus
"Just about every question you've asked over the last 3-4 days has been "urgent". Perhaps a little planning would be helpful?" Colin Angus Mackay in the C# forum
led mike
|
|
|
|
|
mehmetned wrote: why was this error produced?
Because the preprocessor could not find the file.
"The largest fire starts but with the smallest spark." - David Crow
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
You need to make sure the compiler can "see" the files from the library. In the IDE usually under the Tools/Options menu item you can find the VC++ Directory settings. You probably need to add the paths to the library in those settings.
"Just about every question you've asked over the last 3-4 days has been "urgent". Perhaps a little planning would be helpful?" Colin Angus Mackay in the C# forum
led mike
|
|
|
|
|
It's a header file for a POSIX thread library. Where is your code from? POSIX threads have been implemented on Unix, Linux and Windows. Try to figure out what POSIX library yous code was using.
Best,
Jun
|
|
|
|
|
The following doesn't compile:
struct A
{
A* partner;
void doodah()
{ ++partner->value; } //this is legal
protected:
int value;
};
struct B: public A
{
void foobar()
{ partner->value += 2; } //but this isn’t
};
The complaint is that foobar() can't access A::value. But B is an A and A can access the value member of another A. So why can't B - which IS AN A - access the value member of another A?
--Dave Schumann
|
|
|
|
|
i really don't see anything wrong in the code (except the doodah() method is a little dirty, but anyway).
what does the compiler say exactly (code + message) ?
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
|
|
|
|
|
FILENAME.cpp(230) : error C2248: 'A::value' : cannot access protected member declared in class 'A'
FILENAME.cpp(224) : see declaration of 'A::value'
FILENAME.cpp(218) : see declaration of 'A'
line 230 is the "partner->value += 2" call in foobar(). It has no complaint about the "++partner->value" call in doodah().
I'm not a language lawyer but this seems like it should be legal.
|
|
|
|
|
It may be that you are trying to implement class like behaviour using structs.
Try this:
struct A
{
// Let the compiler know that B can access A's protected members.
friend struct B;
A* partner;
void doodah()
{ ++partner->value; } //this is legal
protected:
int value;
};
Dave Kerr
codechamber@hotmail.com
http://www.codechamber.com
|
|
|
|
|
But a class *is* a struct.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
Christian Graus wrote: But a class *is* a struct.
hum, yes, but i would have said here that a struct *is* a class
only a matter of point of view
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
|
|
|
|
|
Internally yes a class is a struct with a vftbl.
Regardless, this does solve the problem. Changing the structs to classes does not, neither does explicitly declaring partner as public.
More explicit casting :
void foobar()
{ ((::A*)this)->partner->value += 2; }
or
void foobar()
{ ((A*)this)->partner->value += 2; }
as mentioned in the notes for Compiler Error C2248 on MSDN doesn't work either. It's rather an odd one.
Dave Kerr
codechamber@hotmail.com
http://www.codechamber.com
|
|
|
|
|
Oh - casting this to an A*. That seems like a really good idea. Pity it doesn't work but it would have made sense if it did .
Yeah I'm confused too. I hope a language lawyer blunders onto this post...I'm wondering whether this is a standards violation or not.
|
|
|
|
|
A "struct" is a class with default public protection. You can change it to a class, put a "public:" at the beginning, and have the same result.
And although in this toy example I could get it to compile with a "friend struct B" declaration, that's stupid. B is a CHILD CLASS. Why should I have to declare it a friend? Why should I even have to know about it? My child classes are supposed to be able to access my protected members.
|
|
|
|
|
my first thought was that A* partner; in A is in the private section of the class, so it is not accessible to B, but you're dealing with struct, and all members in structs should be public; so I'm a little bit confused.
Maximilien Lincourt
Your Head A Splode - Strong Bad
|
|
|
|
|
Dave Schumann wrote: So why can't B - which IS AN A - access the value member of another A?
It's a difference in the way structs differ from classes. If you remove A::value from the protected section, it'll compile.
"The largest fire starts but with the smallest spark." - David Crow
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
Maximilien wrote: all members in structs should be public
no. this is true only is you don't redefine the visibility (which is actually done here with protected: )
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
|
|
|
|
|
>>>It's a difference in the way structs differ from classes
No it's not. Changing these to "classes" with public: at the beginning has no effect (I've tried it)
|
|
|
|
|
Dave Schumann wrote: No it's not.
Sure it is.
Dave Schumann wrote: Changing these to "classes" with public: at the beginning has no effect (I've tried it)
It won't have an effect until you put A::value into a public section. This is the default with a struct . So, you'll either need:
struct A
{
A* partner;
void doodah()
{ ++partner->value; }
int value;
}; or
class A
{
public:
A* partner;
void doodah()
{ ++partner->value; }
int value;
};
"The largest fire starts but with the smallest spark." - David Crow
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
>>>Sure it is.
No - it's not. You said it has something to do with struct vs. class - it doesn't. Change them to clases with public scope at the beginning and everything will be the same. What you did was remove the "protected:" which has nothing to do with it being a struct or not.
I declared "protected:" for a reason, not because I like having things not compile. This is a toy version of a much larger problem. I can't stand posters who post 20 pages of code so I figured I'd boil the issue down. In fact in my case the issue is accessing a *function*...and there's virtual stuff and blah blah blah. Now you're getting bored, appropriately. None of that is relevant to the compiler error which I can't understand.
|
|
|
|