Violet_82 89 Posting Whiz in Training

thanks for the clarifications Narue.

I note you have put

using namespace std;

I noticed you guys on this forum don't use

using namespace std;

but I thought it was more a matter of style other than anything else, what do you mean exactly by

will have their namespace polluted with no warning whatsoever

? what type of warnings? I always thought that it was some kind of shorthand, allowing us to get the rid of the

std::

anytime we want to use cout, cin, endl, therefore I believe I use, maybe partially I am not sure, what's in the std namespace any time I cout something, isn't it the case?

Violet_82 89 Posting Whiz in Training

hi there,
thanks. Following your advice here is what I added:

...
Account money(300) ;

cout << " You can now add some fund to your account. How much do you want to add?\n" ;

while (!( cin >> addBalance ) )
	
	{
	
		cout << " Not a valid input, try again!\n " ;

		cin.clear();

		cin.ignore(INT_MAX, '\n');
		
	}

cin.ignore() ;
...

Just for me to understand it correctly:

with the above code I am testing the input as it comes in. WIth

cin.clear();

what do I exactly achieve?

The

cin.ignore(INT_MAX, '\n');

I read is equivalent to what you Narue used

std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

. I used the other one because it somehow seems easier. I believe what it does is to discard the input up to the maximum int input, is it correct?
Then the libraries to include:

#include <limits>

handles this limit business, but what about

#include <ios>

?

One more question: when we say

while (!( cin >> addBalance ) )

I suppose we validate only the first character, so If I input something like 4a, it will accept it as valid woudn't it?
thanks

Violet_82 89 Posting Whiz in Training

Hi there, it's been awhile :$

I have just completed a small easy exercise which was to create an Account class, providing a constructor to receive the initial account balance, add some money to the initial balance and withdraw some money (providing that the amount of money to withdraw is smaller than the value in the account) It's all good but now I am having some problems in validating the input. Basically when asked to add some money onto the account if I input a char I want my program to tell me that the input is invalid and to try again, but I don't seem to be able to do it. Here is what I have so far:

header file:

//account.h
//this file is the class definition

#include <iostream>

using namespace std ;

const char pound = 156 ;

class Account

{

	public:

		//constructor

		Account( int ) ;
		
		void credit( int ) ;
		
		void debit( int ) ;
		
		int getBalance( ) ;
		
		private: 
		
		int balance ;
		
	};

function definition:

//account.cpp
//this file contains the member function definitions
#include <iostream>

using namespace std ;

#include "account.h" //include the class definition

Account::Account( int initBalance )

		{
		
			if ( initBalance < 0 )

			{

			balance = 0 ; 

			cout << "Initial balance is invalid!\n" ;

			}
		
			else 
				
			{
				
				balance = initBalance ;

				cout << "The initial balance is "<< pound << balance << endl ; 

			}
		}

void Account::credit( int funds )
		
			{
		
				balance += funds ;		

			} …
Violet_82 89 Posting Whiz in Training

Thanks guys.
Arkinder, if I add

{
     margin: 0;
     padding: 0;
   }

then how do I play with margins and paddings? If I set them to 0 then I won't be able to say something like

margin-top:1em;
margin-right:1.5em;
margin-bottom:0;
margin-left:0;

in the code?

MidiMagic, if different browsers then apply different settings, the only way to get around will be, as I was suggested, to use a different stylesheet for IE then. Like the problem I am having with the horizontal line that is not centred in IE but it is in firefox et al

Violet_82 89 Posting Whiz in Training

Negative? Ok cool, will definitely try that and then post back, thanks, didn't know I could use negative values

Violet_82 89 Posting Whiz in Training

I see, that makes sense, and does it need to go before or after the main CSS?
The only problem with this is that I wouldn't be able to put anything in it because I don't know how to make thing work in IE7. Take the first spacing issue: in the last code I posted (I removed the bottom margin so that there shouldn't be any space between the horizontal line and the horizontal navigation as it happens in firefox) IE7 still adds some space. I tried pretty much everything I could think of, but still can't remove the extra space.
Or take the second issue: the horizontal line that in IE7 is in the middle of the page: again, in the last code I posted I set

float:left;

in line 38, so if it doesn't float when I gave it that value, how do I make it float it left? I thought of "align" but that doesn't work either...

Violet_82 89 Posting Whiz in Training

I see, so what would you recommend then?

Violet_82 89 Posting Whiz in Training

I did yes, it's in line 18 of the css. I gave it 0 now because I was trying to determine how much more space IE7 gives it to the gap between the line and the navigation. I can give it any value really, still IE7 adds that little annoying bit.
The funny thing is though, that's only IE7, whereas IE8 is absolutely fine.
I am also floating the horizontal line left (line 38 in the CSS) and guess what? it is fine in all browsers (included IE8) but in IE7 the line doesn't float left but it is in the middle of the page and I can't get it to float left! how annoying

Violet_82 89 Posting Whiz in Training

Hi there,
I changed them but it doesn't seem to make much difference, also the margins I am interested in have all value 0 (so does it matter whether is em or px?).

However, I solved one of the above problems, the space between the left corner and the top navigation. the code I had was

#topnav ul
{
list-style: none;

margin-left:10px;
}

but apparently firefox and chrome adds some extra padding (!!??), so I added this line

padding-left:10px;

to make it even and now it works! yay! Unfortunately I couldn't solve the other problem (the space between the horizontal navigation and the horizontal line). I applied pretty much the same logic even if this time IE7 seems to add some extra space between the two. I tried to even up the space removing any possible extra margin or padding and some extra modifications ending up with this

/*This is the top navigation */
 
#topnav ul
{
list-style: none;

margin-left:10px; /*to have some space on the left of the top navigation, but it is rendered differently in IE and firefox */
padding-left:10px; /*the above problem is resolved adding the padding. Firefox and chrome seem to add some extra padding on their own */ 
margin-bottom:0;
padding-bottom:0;
}
 
#topnav li
{
float: left;
margin-top:1em;
margin-right:1.5em;
margin-bottom:0;
margin-left:0; /*bottom margin is because I want some space between the top navigation and the horizontal line but it is rendered differently in IE and firefox */
padding-bottom:0;
font-family: Arial, Verdana, …
Violet_82 89 Posting Whiz in Training

Ikunu,
thanks for your reply, but I am not sure what change the DOCTYPE declaration could do to the margin. Also, I would like to use the "strict" and not "transitional" because I want to make sure I don't use deprecated tags
Could you think of anything else that affect the margins (I insist on margins because I think the problem somehow depends on the margins, but I might be wrong)

Violet_82 89 Posting Whiz in Training

Hi guys,
I seem to have a problem with the margins in the website I am developing. The spacing looks different in IE and Firefox and I don't understand why.
here is the CSS code:

h1
{
text-align:left;
font-size: 1.7em;
font-family: Arial, Verdana, sans-serif;
}

#banner
{
background:#81A594 url(typewriter.jpg);
background-repeat:no-repeat;
background-position:right;
height:120px;
padding:10px 50px 0 200px;

}
/*This is for the left hand side navigation */

#navigation ul /* for the navigation, foreground is a2c1b2 and bg is f5d9d7*/
{
list-style:none;
padding:0;
border:0;
margin:4em 0 0 0;
}

#navigation ul li
{
background: #f4d8c7 url("box.jpg") repeat-x scroll center bottom;

width: 10em;
display: block;
text-decoration: none;
text-align: center;
font-family: Arial, Verdana, sans-serif;
padding:10px 0 10px 0;
}

/*This is the top navigation */

#topnav ul
{
list-style: none;
margin-left:10px; /*to have some space on the left of the top navigation, but it is rendered differently in IE and firefox */

}

#topnav li
{
float: left;
margin: 0 1.5em 1em 0; /*bottom margin is because I want some space between the top navigation and the horizontal line but it is rendered differently in IE and firefox */
font-family: Arial, Verdana, sans-serif;
}
body
{
background-color:#E6E6DC;

}

/* Horizontal line */

#horizontal_line
{
clear:both;
color:#81a594;
background-color:#81a594;
height:0.35em
}

and here is the page I am working on:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Web editing in Richmond</title>
<link rel="stylesheet" type="text/css" href="style.css" >
</head>
<body>

<!-- BANNER STARTS HERE -->
<div id="banner">
<h1>Web editing …
Violet_82 89 Posting Whiz in Training

thanks :), will need to do some work on this website, so will post more stuff soon. thanks for your help

Violet_82 89 Posting Whiz in Training

Hi there, yes the problem is the height. I have done what shaya4207 suggested and adjusted the line-height and height, in fact I removed altogether and now it's neater and no space between the buttons. I thought I had to have height and line-height. Did they mess my buttons up because the image I use for the buttons (which is attached in my first post next to the typewriter on the left is 6px x 40px? )

About the padding

You gave it a 'padding' of '10px 0 10px 0', which if I'm not mistaken gives it 10px on top and bottom

, yes that's right if I don't do that the buttons appear really tiny and the text a bit squashy inside. Also

Why do you have in your CSS both '#navigation li' and '#navigation ul li'

I removed

#navigation li{margin: 0 0 0 0;border: 0 0 0 0;}

. Iput it there becasue i read about it somewhere, I think it was here http://www.webcredible.co.uk/user-friendly-resources/css/css-navigation-menu.shtml

Sorry forgot to include the new CSS:

h1
{
text-align:left;
font-size: 1.7em;
font-family: Arial, Verdana, sans-serif;
}

#banner
{
background:#81A594 url(typewriter.jpg);
background-repeat:no-repeat;
background-position:right;
height:120px;
padding:10px 50px 0 200px;

}
/*This is for the left hand side navigation */

#navigation ul /* for the navigation, foreground is a2c1b2 and bg is f5d9d7*/
{
list-style: none;
padding: 0;
border: 0;
margin: 4em 0 0 0;
}

#navigation ul li
{
background: #f4d8c7 url("box.jpg") repeat-x scroll center bottom;

width: 10em;
display: …
Violet_82 89 Posting Whiz in Training

Hi guys,
I am having some problems with the navigation of this website I am involved with.
The issue I have is on the left hand side navigation. I can't get the buttons to sit neatly on the top of each other, it looks like there is some space between them (you can in fact see the background colour).
I have tried to use the padding option (I used a lot the W3C school website) but I don't seem to be getting it right. Is it an issue with the padding or what?

Here is the code for the CSS:

h1
{
text-align:left;
font-size: 1.7em;
font-family: Arial, Verdana, sans-serif;
}

#banner
{
background:#81A594 url(typewriter.jpg);
background-repeat:no-repeat;
background-position:right;
height:120px;
padding:10px 50px 0 200px;

}
/*This is for the left hand side navigation */

#navigation ul /* for the navigation, foreground is a2c1b2 and bg is f5d9d7*/
{
list-style: none;
padding: 0;
border: 0;
margin: 4em 0 0 0;
}

#navigation li
{
margin: 0 0 0 0;
border: 0 0 0 0;
}

#navigation ul li
{
background: #f4d8c7 url("box.jpg") repeat-x scroll center bottom;
height: 2.4em;
line-height: 3.5em;
width: 10em;
display: block;
text-decoration: none;
text-align: center;
font-family: Arial, Verdana, sans-serif;
padding:10px 0 10px 0;
}

/*This is the top navigation */

#topnav ul
{
list-style: none;
margin-left: 0px; 

}

#topnav li
{
float: left;
margin: 0 1.5em 0 0;
font-family: Arial, Verdana, sans-serif;
}
body
{
background-color:#E6E6DC;

}

And here is the code for one of the …

Violet_82 89 Posting Whiz in Training

thanks jonsca, sorry for the late reply, but my circumstances have changed a bit, I withdrew from my software engineering course, so I guess I won't be troubled by the rectangle problem for awhile. I want to concentrate on classes and constructors now that I have more time :)

Violet_82 89 Posting Whiz in Training

Hi there,
thanks for that and I had a look at the other tutorial, good stuff. The thing is that I am pretty new to Css and HTML, so still trying to get my head around it.
So in the example below the anchor is absolutely necessary, is there any way taht I can avoid using it, as in something like

#nav-menu li
{...
Violet_82 89 Posting Whiz in Training

Hi there,
could anybody kindly tell me what the "a" (in the 1st line)is in this css and what is used for please? It is driving me insane and I can't find anything anywhere (apart from the w3c schools that says that the a is to order things alphabetiacally??)

#nav-menu li a
{
background: url(background.gif) #fff bottom left repeat-x;
height: 2em;
line-height: 2em;
float: left;
width: 9em;
display: block;
border: 0.1em solid #dcdce9;
color: #0d2474;
text-decoration: none;
text-align: center;
}

cheers

Violet_82 89 Posting Whiz in Training

thanks :)

Violet_82 89 Posting Whiz in Training

HI thanks for your replies and suggestions. I noticed the

min-width

and yes the navigation goes crazy if I put that in. Anyway, I found another way to have the text back without removing

height: 2em

, but instead in

.left ul a{

changing

width: 9em;

to

width: 12em;

so that the navigation "boxes" become a bit bigger and the text doesn't go onto the new line.

After I made the changes though the navigation stopped working in IE8 as in some of the links in the navigation were not going anywhere, and it was driving me absolutely crazy. Then when I put it online at http://www.antobbo.webspace.virginmedia.com/index.htm all the problems disappeared...odd. Anyway I will also change the ">" you mentioned before, would it cause a problem if I leave it there or is it just good practice to remove it?

Violet_82 89 Posting Whiz in Training

hi guys, I am working on a project for my school, and I was wondering whether I can get some guidance and tips on how to tackle it.
Here’s the problem in brief:
I was asked to develop a program so that users can input the data values of 2 rectanglees and 4 points and using operator function to find out whether the 2 rectangles are the same (using the area of rectangles) and if not which of the two is bigger, also determine whether the point mentioned above are within the rectangle or outside and output the answer. The hints I am given are to use 2 classes: one for the points (using constructors - copy, explicit etc (?) - , member functions - operator functions(?)) - and one for the rectangle – containing coordinates (4), width and height, constructors and operators functions. The x and y (top left coordinates 0,0) are coordinates of the GUI windows.

Now, is it a good idea to start by drawing all this do you think?

thanks

Violet_82 89 Posting Whiz in Training

Hi there,
thanks for your suggestions, I will try that and make the amendments when I get home tonite. Unfortunately the site is not live as yet no, but I will definitely post the link when we release it if the problem is still there

Violet_82 89 Posting Whiz in Training

Hi guys,
I am having a nightmare with a small website I have built.
Here's the visual_impairments.htm page:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<head>
<title> Visual impairments </title>
<link rel="stylesheet" type="text/css" href="style.css" >
</head>
<body>
<div class="banner">
<h1>IT and Disability</h1>
</div>

<!-- BREADCRUMB TRAIL STARTS HERE, EVERY PAGE EXCEPT THE HOME PAGE SHOULD HAVE THIS!! -->
<p>You are in: <a href="index.htm">Home</a> > Visual impairments </p>

<!-- BREADCRUMB TRAIL STOPS HERE-->


<hr style="height:5px;border-width:0;color:red;background-color:red;" />


<!--PATRICK'S NAVIGATION-->

<div class="left"> 
<div class="right">

<div id="content">

<ul>
<li><a href="index.htm">Home</a></li>
<li><a href="visual_impairments.htm">Visual impairments</a></li>
<li><a href="human_input_devices.htm">Human input devices</a></li>
<li><a href="topic3.htm">Topic 3</a></li>
<li><a href="topic4.htm">Topic 4</a></li>
<li><a href="references.htm">References</a></li>
<li><a href="group_members.htm">Group members</a></li>
</ul>
</div>

<div style="clear: both;">
</div>
</div>
</div>

<!--PATRICK'S NAVIGATION ENDS HERE-->

<!-- MAIN CONTENT STARTS HERE  -->


<h2>Here goes your main heading </h2>

<p class="paragraph">My first paragraph y first paragraphy first paragraphy first paragraphy first paragraphy first paragraphy first paragraphy first paragraphy first paragraphy first paragraphy first paragraphy first paragraphy first paragraphy first paragraphy first paragraph.</p>

<p class="paragraph"> Other paragraph </p>

</body>
<!-- MAIN CONTENT ENDS HERE  -->

</html>

And here is the CSS:

h1
{
text-align:center;
}

h2
{
text-align:center;
}

h3
{
text-align:center;
}



.paragraph
{
margin-right:100px;
margin-left:200px;
}

.list
{
margin-right:100px;
margin-left:200px;
}

#content {
padding: 10px 0px;
}

.right {
width: 600px;
float: right; 
width: 85%;
text-align: left;
}
.right h2 {
color: #FF4800;

padding : 10px 0 15px 0;
}

.left {
width: 130px;
height: 180px;
float: left;
padding: 1px;

}

.left ul{ …
Violet_82 89 Posting Whiz in Training

sorry didn't mean to be confusing. My original request and this one are in fact the same pretty much, it's just the examples I used that are different. I fully understand now my own example (the one with the cost function) and the next example was just to confirm that I understand how to deal with this precision business.

I have few numbers the program takes from a file, and they are:
3.78
6.658
4.4569
5.76549
3.665439

The problem I was having is when I cout them. With no precision set, the screen outputs this:

3.78
6.658
4.4569
5.76549
3.66544

rounding the 6th digit after the period in the last number.

With the fixed precision set to 6

cout<<fixed<<setprecision (6)...;

it displays

3.780000
6.658000
4.456900
5.765490
3.665439

which is what i expected but the trailing zeroe are a bit annoying so I was trying to find a way to get the rid of them. So reading the tutorial and your post I now know that the one to use is the default setprecision, so

cout<<setprecision (7)...;

because the default one takes into consideration numbers before and after the period and since 3.665439 is 7 digit I need 7. With this my output is sorted:
3.78
6.658
4.4569
5.76549
3.665439

What's annoying though, is that you need to know how many digits your …

Violet_82 89 Posting Whiz in Training

thanks guys. Jonsca sorry to labour the point but, to go back to the floating point issue, I had a look again at http://www.cplusplus.com/reference/iostream/manipulators/setprecision/ and the example there is:

// setprecision example
#include <iostream>
#include <iomanip>
using namespace std;

int main () {
  double f =3.14159;
  cout << setprecision (5) << f << endl;
  cout << setprecision (9) << f << endl;
  cout << fixed;
  cout << setprecision (5) << f << endl;
  cout << setprecision (9) << f << endl;
  return 0;
}

which prints
3.1416
3.14159
3.14159
3.141590000

Now, the first cout is set to 5, so shouldn't the first result have 5 digits and be 3.14159? why is it rounding it? Same story for the second cout which is set to 9, so shouldn't the second result have 9 digits rather than 5? the "fixed" I think is clear enough.

I am finding myself in this situation at the moment. I have a program which takes 5 floating numbers as input from a file say 3.78, 6.658, 4.4569, 5.76549, 3.665439. I set

cout<<"numbers to be processed are: "
<<fixed
<<setprecision ( 6)
<<numbers;// to indicate the numbers to be output

and with a for loop it prints all the above numbers. Obviously it displays:
3.780000
6.658000
4.456900
5.765490
3.665439

Any way to get the rid of the zeros but at the same time making sure that the last number prints all …

Violet_82 89 Posting Whiz in Training

Hi thanks, well I don't use 'printf ()' because to be honest I have never come across it before...do you have any link where I can read a bit more about it?

Violet_82 89 Posting Whiz in Training

jonsca,
thanks for that. I had a read at the tutorial, not sure if I understand it correctly though especially when it comes down to distinguish between fixed and default...
this is what I came up with, and it works:

#include<iostream>
#include <iomanip>
using namespace std;
double cost;
double getCost( )
 
			{
 
				return cost ;
 
			}
 
		void setCost( double Cost ) 
 
			{
 
				cost = Cost ;
 
			}
 
int main()
 
{
	double myCost = 0.0 ;
 
	cout << "What's the cost? ";
 
	cin >> myCost ;
 
	setCost(myCost) ;
 
	cout <<"the cost is";
	cout<< fixed;
	cout<< setprecision (2)<< getCost() ;
 
 
 
}

Just so I understand this properly:

cout<< fixed;//not sure what it does
cout<< setprecision (2)<< getCost() ;

This instead effectively says that I want the floating number to display 2 digits after the period..is it correct?

Violet_82 89 Posting Whiz in Training

hi there, sure here is an example:

#include<iostream>
using namespace std;
double cost;
double getCost( )

			{
				
				return cost ;

			}

		void setCost( double Cost ) 

			{
				
				cost = Cost ;

			}

int main()

{
	double myCost = 0.0 ;

	cout << "What's the cost? ";

	cin >> myCost ;

	setCost(myCost) ;

	cout <<"the cost is"<< getCost() ;



}

if I input 7.99 it returns 7.99, if I input 7.00 it returns 7 and not 7.00 as I would like. Why is that and how do I get around that...really don't have a clue sorry and can't think of anything...
thanks

Violet_82 89 Posting Whiz in Training

Hi ya, no I don't convert it into an int. If I replace that 7.00 with say 7.01 then when the function returns it will return 7.01. So for some reasons when it is 7.00 it chops off the .00.

The variable cost has been declared as double. I have another function that sets the cost, say something like

void setCost( double Cost ) 

			{
				
				cost = Cost; //cost is a double here

			}

I pass a value of 7.00 to this function and then somewhere else in main I call the double MyPrice function so it should return 7.00 and not 7. As I said above if I change that 7.00 to 7.01 or any other value as long as the last digit is not 0 will return fine. So say 7.02 etc.

Violet_82 89 Posting Whiz in Training

Hi there, if I call a double function like

double MyPrice( )

			{
				return cost;
			}

(value "cost" assumed to be set to 7.00 why this function returns 7 and not 7.00?

thank you

Violet_82 89 Posting Whiz in Training

great, thanks for clarifying it, that's really helpful!

Violet_82 89 Posting Whiz in Training

Ok I see but how do I determine whether a file needs to be compiled or not? I mean in my example I have 3 files if you remember: the header file which obviously doesn't need to be compiled because I assume it does what it does at linking time, the .cpp file with the main function which needs to compile and the second .cpp with the functions implementations which, again, needs to be compiled together and at the same time as the other .cpp one. So a .cpp file always need to be compiled then, even if it holds the functions implementations only. Is there any case when a .cpp file doesn't need to be compiled?
thanks

Violet_82 89 Posting Whiz in Training

Brilliant, thanks a lot : ). I just thought that adding 2 files to the same "folder" could have caused some problems to the program, but I suppose then that I can add as many files as I need to each folder and then compile just the file with main in it.

Violet_82 89 Posting Whiz in Training

Brilliant mitrmkar,
thanks for that, I see, I was trying to use a header file that in fact doesn't exist!

thanks again

Violet_82 89 Posting Whiz in Training

Hi there,
I have a question about header files/source files in Visual C++ (I use visual c++ 2005 express).

I used to put all my code in one single .cpp file but now I have a test program (done by my tutor at Uni) which has 3 files: 2 .cpp (one has the main function, and the second one has got the functions implementations) and a .h file with the function headers only.
Now, I know that all these files need to be in the same folder for the program to work, which is quite easy to do in Unix using g++, but with Visual is another story.

What I usually do when I write a program is to start a new empty project (for now still a Win32 console application) so that in Visual, in solution explorer, I have in order:
Header files
Resource files
Source files

What I have done with the 3 files that I have, after starting a new project, was to add my .h file into the "header file" section (by right clicking on header files, add, new item) and the 2 .cpp files into the "source files" section (again by right clicking on header files, add, new item: done it twice, one for each .cpp file). I run the program and it works fine, but I wonder whether this is the right way to deal with programs with multiple files or not.

as usual, any …

Violet_82 89 Posting Whiz in Training

ok, thanks for that

Violet_82 89 Posting Whiz in Training

Hi there, I have a question about a really simple program. The code below doesn't work, the compiler comes back with "fatal error C1083: Cannot open include file: 'ofstream': No such file or directory":

#include <iostream>

#include <ofstream>

using namespace std ;

int main( )

{

ofstream myfile("newfile.txt") ;

myfile << "This file contains this sentence.\n" ;

myfile.close() ; 


}

if I instead replace

#include <ofstream>

with

#include <fstream>

it's all good.
I thought that if I want to create a file and input some data in it ofstream was enough, or if I wanted to read from a file I had to use ifstream. fstream, I thought, needs to be used only if I want to read and write...
Any suggestion? thanks

Violet_82 89 Posting Whiz in Training

hi thanks, yeah that's what I thought, but I was not sure. SO the try catch itself is fine but when I get

exit(0);

I need cstdlib. Cool, thanks guys

Violet_82 89 Posting Whiz in Training

Hi guys,
quick question. Does the structure

try
{

}

catch

{

exit(0);
}

require the preprocessor directive

#include<cstdlib>

to be declared at the beginning of the program? The thing is if I don't declare it the visual c++ compiler works fine but if I try in Unix with g++ it doesn't compile...
thanks

Violet_82 89 Posting Whiz in Training

I see, thanks for your help

Violet_82 89 Posting Whiz in Training

oh I see, it makes sense yes thanks. But what if I want to keep the int array? Do you think i could do that and maybe use a cast construct? Or is it not an option?

Violet_82 89 Posting Whiz in Training

hi ya,
thanks.
DO you mean something like

#include<iostream>

 
using namespace std;
 
int main()
{
 
char numbers[5];
int k;
 
cout<<"Enter 5 numbers: ";
 
for( k = 0; k<5; k++)
{
cin >> numbers[k];
while((numbers[k]<46) || (numbers[k]>58))
	{
		cout<<"wrong, again ";
		cin >> numbers[k];
	}
}
cout<<"here are your numbers: ";
for (k=0; k<9; k++)
{
cout<<" "<<numbers[k];
}
 
}
Violet_82 89 Posting Whiz in Training

Hi all,
I am trying to make sure that users can only input integers in my program and no other types. Here is the program:

#include<iostream>

using namespace std;

int main()
{

int numbers[9];
int k;

cout<<"Enter 9 numbers: ";

for( k = 0; k<9; k++)
{
cin >> numbers[k];
}
cout<<"here are your numbers: ";
for (k=0; k<9; k++)
{
cout<<" "<<numbers[k];
}

}

I am having real trouble (I am still a beginner) to validate user input.
I have tried with the

isdigit()

function, with if

((numbers[k]<'48' || numbers[k]>'58'))

because I thought that the ascii table would have been of use but no joy. All the above included in a while loop of course...

I meant I tried things like:

while((numbers[k]<'48' || numbers[k]>'58'))
	{
		cout<<"wrong, again ";
			cin >> numbers[k];
	}

or

while((!isdigit(numbers[k]))
	{
		cout<<"wrong, again ";
			cin >> numbers[k];
	}

Any suggestion?
thanks

Violet_82 89 Posting Whiz in Training

thanks, I spent ages changing my program, debugging it and it was a little silly glitch.
Cheers, now it is clear.

Violet_82 89 Posting Whiz in Training

ah, yes I see now! So I was trying to copy from word to word rather than from test to word. I wanted to have word changed but I was changing it the wrong way!
thanks

Violet_82 89 Posting Whiz in Training

Hi, thanks. Well when I said

word.at( i ) = word.at( j );

was because I wanted to copy the reversed word back into the same string, which is why I said

test = word ;

, so to keep my original word and then eventually compared with the reverse

if (test == word)

. I actually did try before posting to do what you suggested but the program doesn't run, it aborts, it says that there is an error even if it compiles fine.
What really bugs me is that if say I input the word "attat" in my program. It works fine in a way, because it doesn't recognise it as a palindrome, but when it gets to

out << "Original Word is " << test << "\n and new word is " << word << endl;

the output is for the original word "attat" - as you would expect - but for the new word is instead "tatat". This happens with every word apart from polindromes...and I can't get why...

Violet_82 89 Posting Whiz in Training

Hi there,
I am working on a palindrome program which is here:

#include <iostream>

#include <string>

using namespace std ; 

int main( )

{
	cout << "Insert the word to test: " ;

	string word ;

	string test ;

	int i ;

	int j ;

	cin >> word ;

	

	cout << "The word you input is " << word << " " << endl ;

	test = word ;
	
	for ( i = 0 ,  j = ( word.size() -1 ); i < word.size(), j >= 0 ; i++, j-- )
		{
	
			
			word.at( i ) = word.at( j );
						

		}
cout << "Original Word is " << test << "\n and new word is " << word << endl;
	if (test == word) cout << "Palindromes!";
	else cout<<"wrong!" ;



}

Now, it compiles fine and it even works but this line

cout << "Original Word is " << test << "\n and new word is " << word << endl;

showed me that the program doesn't actually works in a sense because for whatever reason in the process of copying from

word.at( i ) = word.at( j );

the character change...When I compile it the compiler returns 2 warnings, but got no idea what that means...any idea?
thanks

warning C4267: '=' : conversion from 'size_t' to 'int', possible loss of data
warning C4018: '<' : signed/unsigned mismatch

Violet_82 89 Posting Whiz in Training

Ah!!! hold on, sorry, I think I got it now...sorry took me awhile, but you see I am still trying to get the hang of it. In

for ( Student* student_ptr = studentSet ;
student_ptr < &studentSet[ NUM_STUDENTS ] ;
student_ptr++ )

we must say

student_ptr < &studentSet[ NUM_STUDENTS ]

because the pointer points to the address of

studentSet[0]

and it must be obviously smaller than

&studentSet[ NUM_STUDENTS ]

which points to the address of

studentSet[3]

which is outside my array - it is out of range.
Sorry didn't mean to waste anybody's time.
cheers

Violet_82 89 Posting Whiz in Training

Hi Kontained, thanks, yeah that's clear but in

for ( Student* student_ptr = studentSet ;
student_ptr < &studentSet[ NUM_STUDENTS ] ;
student_ptr++ )
{

what's not clear is

&studentSet[ NUM_STUDENTS ]

. We know that

student_ptr

is a pointer and contains the address of

studentSet[0]

so what address does

&studentSet[ NUM_STUDENTS ]

contain? the address of

studentSet[2]

I suppose because if we say

]for ( Student* student_ptr = studentSet ;
student_ptr < &studentSet[ NUM_STUDENTS ] ;
student_ptr++ )

we don't want the address of the pointer to go past the address of

tudentSet[2]

which contains the last element of the array...I would have thought

Violet_82 89 Posting Whiz in Training

Hi guys, I have a problem with a program:

#include <iostream>
#include <string>
using namespace std ;
// Uses a class with "public" member variables, so directly accessible
class Student
{
public :
string name ;
int reg ;
} ;
int main( )
{
const char NL = '\n' ;
const int LINE_LENGTH = 80 ;
const int NUM_STUDENTS = 3 ;
Student studentSet[ NUM_STUDENTS ] ;
cout << "For the set of students "
<< endl ;
for ( int i = 0 ; i < NUM_STUDENTS ; i++ )
{
cout << "Enter the name of student number "
<< i + 1
<< ": " ;
getline( cin, studentSet[ i] . name ) ;
cout << "Enter this student's registration ID: " ;
cin >> studentSet[ i ] . reg ;
cin . ignore( LINE_LENGTH, NL ) ;
cout << endl ;
}
//cout<<"Address " << &studentSet[0]<< &studentSet[1] << &studentSet[2] << &studentSet[3];
for ( Student* student_ptr = studentSet ;
student_ptr < &studentSet[ NUM_STUDENTS ] ;
student_ptr++ )
{
cout << "Name of a student: "
<< student_ptr -> name
<< endl ;
cout << "Student registration ID: "
<< student_ptr -> reg
<< endl
<< endl ;
}

}

My problem is with the pointer in the loop

for ( Student* student_ptr = studentSet ;
student_ptr < &studentSet[ NUM_STUDENTS ] ;
student_ptr++ )

Now, I am trying to get my head around pointers, and I must admit that I am finding it …

Violet_82 89 Posting Whiz in Training

oh I see,so if I place the increment after the counter I will be able to use 26. I will have a go and post the result, thanks