I/O stream problems

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1218 Views - Last Post: 27 November 2007 - 02:19 AM Rate Topic: -----

#1 Lt_Kernel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 05-September 07

I/O stream problems

Post icon  Posted 18 November 2007 - 11:19 AM

 #include <iostream>

using namespace std;

int main()
{
	float height;
	int bottom, center, top;
	
	cout <<"enter height\n";
	cin >>height>>endl;
	cout <<"enter bottom\n";
	cin >>bottom>>endl;
	cout <<"enter top\n";
	cin >>top>>endl;
	
	for (bottom = 0, top = 0, center = height/2; bottom == top, top == bottom, center == height/2; bottom+=135,top+=135,center/2)
				
				if ( bottom==135 mm, top==135 mm)
		   
		   {
				cout << "marking hole\n"; }
				cin.get ();
				}
				


this is my first
please help make it work

note C:\Dev-Cpp\include\c++\3.4.2\bits\istream.tcc:87 candidates are: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>&(*)(std::basic_istream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]

one of the errors

This post has been edited by Lt_Kernel: 18 November 2007 - 11:20 AM


Is This A Good Question/Topic? 0
  • +

Replies To: I/O stream problems

#2 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 855
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: I/O stream problems

Posted 18 November 2007 - 11:40 AM

	cin >>height>>endl; 


Check this line and your use of endl. Think about what you're trying to do here and what endl is for (hint : endl is used to output a newline to the an output stream, and flush its buffer. cin is an input stream).

You seem to have done this in several places.


edit - Also, check your use of the comma operator as used in your for-loop condition, and if conditional. I strongly suspect that the code doesn't do what you think its doing, suggest you look up conditional logic operators for 'and' and 'or' (&& and || )

This post has been edited by Bench: 18 November 2007 - 11:52 AM

Was This Post Helpful? 0
  • +
  • -

#3 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: I/O stream problems

Posted 18 November 2007 - 02:22 PM

View PostBench, on 18 Nov, 2007 - 11:40 AM, said:

edit - Also, check your use of the comma operator as used in your for-loop condition, and if conditional. I strongly suspect that the code doesn't do what you think its doing, suggest you look up conditional logic operators for 'and' and 'or' (&& and || )

The comma operator is an interesting little thing in C/C++. It allows you for special expressions that evaluate "sub expressions". It is really an oddity.

<comma expression> = <expression1>, <expression2>

<expression1> is evaluated, and the value is discarded, then the <expression2> is evaluated and its value is assigned to the overall value of the expression (i.e. value of <comma expression> == <expression2>.

So in the for-loop if you use the comma operator you have to be careful:
for(i=0, j=0; i < 2, j < 5; i++, j ++) { whatever }


What is the condition? well it will evaluate 'i < 2', ignore it, and then look at 'j < 5'. So the loop condition is: "loop as long as j < 5"

So to make the code work the way we might expect we need to use the operators && or ||

i < 2 && j < 5 -- both conditions must be met
or
i < 2 || j < 5 -- either condition must be true

Note that the comma operator works great in the first and last expression of the for-loop.

The comma operator is very useful, but it can be a little on the confusing side.
Was This Post Helpful? 0
  • +
  • -

#4 Lt_Kernel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 05-September 07

Re: I/O stream problems

Posted 19 November 2007 - 10:04 AM

 #include <iostream>


using namespace std;

int main()

{
	
	int bottom, center, top, height;
	
	
	cout <<"enter height\n";
	cin >>height;
	cout <<"enter bottom\n";
	cin >>bottom;
	cout <<"enter top\n";
	cin >>top;
	
	for (bottom = 0+12, top = 0+41, center = height/2; bottom < top && top > bottom && center == height/2; bottom==,top==,center/2)
				
				if ( bottom==135 + 12 && top==135 + 41 && center==height )
		   
		   {
				cout << "marking hole\n"; }
				cin.get ();
				} 


thats the change and below are the errors

20 C:\Dev-Cpp\Untitled1.cpp expected primary- expression before ',' token
20 C:\Dev-Cpp\Untitled1.cpp expected primary-expression before ',' token

C:\Dev-Cpp\Untitled1.cpp In function `int main()':
C:\Dev-Cpp\Makefile.win [Build Error] [Untitled1.o] Error 1

This post has been edited by Lt_Kernel: 19 November 2007 - 10:06 AM

Was This Post Helpful? 0
  • +
  • -

#5 Lt_Kernel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 05-September 07

Re: I/O stream problems

Posted 19 November 2007 - 11:57 AM

for (bottom = 135+12; bottom<top; bottom==bottom)
	 for (top = 135+41; top>bottom; top==top)
	 for (center =height/2; center==height/2;center==height/2)


i did like this and it works
but the infinite loops is my problem now
Was This Post Helpful? 0
  • +
  • -

#6 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: I/O stream problems

Posted 19 November 2007 - 12:04 PM

You never increment your top or bottom variables, therefore the loops will not break.
(center =height/2; center==height/2;center==height/2)


I'm not sure what this loop is meant to do. Can you explain?
Was This Post Helpful? 0
  • +
  • -

#7 Lt_Kernel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 05-September 07

Re: I/O stream problems

Posted 19 November 2007 - 12:33 PM

for (bottom = 0; bottom<top; bottom+12)
	for (top = 0; top>bottom; top+41)
	for (center =0; center==height/2; center==height/2)
	 
			if (bottom=135+12, top=135+41, center=height/2);					 
		   {
				cout << "marking hole\n";
							   
		   }  
			  
		   system ("pause");
				return 0;
			}


this is the change and no more infinite loop

I'm not sure what this loop is meant to do. Can you explain?


condtion must be equal to h/2 & there us no increment since center is center and center is not bottom nor top its center the exact position is center it doesn't go one milimeter above or below it never
and since i put some cin for height i figured it start at half of it

but i have changed it with the code above

the problem now is how am i supposed to know if this thing made it correct?
e.g. 135+12 = 147 , or height = 2000mm/2 = 1000mm

all i see is "marking hole"

how can i make it to add the cin and pre given value?

the code below

#include <iostream>
#include <cmath>


using namespace std;

int main()


{ 
	
	int bottom, center, top, height,counter;
	counter = 0;
	cout <<"\n\n\n";				  " UPVC DOORFRAME HINGES\n";	
	
	cout <<"enter height\n";
	cin >>height; 
	 cout <<"enter bottom\n";
	cin >>bottom; 
	 cout <<"enter top\n";
	cin >>top; 
	cout<<"\n";   
	
	  counter =0;
	  
	for (bottom = 0; bottom<top; bottom+12)
	for (top = 0; top>bottom; top+41)
	for (center =0; center==height/2; center==height/2)
	 
			if (bottom=135+12, top=135+41, center=height/2 >>counter++ );					 
		   {
				cout << "marking hole" << counter <<"\n";
							   
		   }  
			  
		   system ("pause");
				return 0;
			}

This post has been edited by Lt_Kernel: 19 November 2007 - 12:50 PM

Was This Post Helpful? 0
  • +
  • -

#8 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: I/O stream problems

Posted 19 November 2007 - 01:12 PM

if (bottom=135+12, top=135+41, center=height/2 >>counter++ );


Can you explain what you wish to add exactly? What values? I'm not even sure what functionality you are trying to produce...for example:
bottom=135+12


why not say
bottom=147


Do you want to use the assignment operator there? Meaning you wish to assign the value of 147 to bottom? Or do you want to compare the value of bottom to 147 using the equality operator? Also, you've ended your if statement with a semi colon, so this is not likely doing what you think it may be.

You have provided some code and a few error messages. Can you explain exactly what this code SHOULD be doing? What is it's intent?
Was This Post Helpful? 0
  • +
  • -

#9 Vick7  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 01-November 07

Re: I/O stream problems

Posted 19 November 2007 - 01:16 PM

I'm not sure what kind of errors you are getting, but you may want to stringstream the upper parts of this code. That is what I would use.
Was This Post Helpful? 0
  • +
  • -

#10 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 855
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: I/O stream problems

Posted 19 November 2007 - 04:55 PM

Its not clear (at least not to me) exactly what you're trying to do with your program, if you could explain to us in layman's terms what you expect your program to do, (eg, what the output is that you're expecting) then we may be able to point you in the right direction

This post has been edited by Bench: 19 November 2007 - 04:58 PM

Was This Post Helpful? 0
  • +
  • -

#11 Lt_Kernel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 05-September 07

Re: I/O stream problems

Posted 20 November 2007 - 11:04 AM

a rectangle vertically positioned with an elevation of 2000 mm (2mtr) or any size at what the cin gets plus a retangle inside of it is my target.
the 1st rectangle is smaller than the the one inside (2nd), this is like your door (there is sash and frame), the cin requires the height of the first and devides and gives the center then it goes to bottom with a fix measurement of 135mm and so as to the top.
the 2nd retngle (the one inside) is bigger by 12mm + 41mm thats for the topbottom and top respectively. thats my problem if i put top++ then it would add and can't get the top correctly, and an infinite loop appears, as it is; if i put a fix value then theres no loop; if i put top== there is error

now what i want to do is put a mark on the second rectangle (the one inside) giving the value of the 1st (the one on top)
then i also need to read the report (cout) if correct so that there is no need for me to put an "else" and ask if correct or not
but first it had to ask how many have to be made 20 doors or higher would be great

but anyway i,m also trying my best here thanks
Was This Post Helpful? 0
  • +
  • -

#12 Lt_Kernel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 05-September 07

Re: I/O stream problems

Posted 20 November 2007 - 01:41 PM

 #include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string>


using namespace std;

int main()

{		 
	int bottom, center, top, height,quantity,fix,fix2,fix3;
	char ans;
				char getchar();
	ans=getans();
	top = fix+41;
	bottom =fix2+12;
	center = height/2;
	fix = 138;
	fix2 = 135;

	
	
	
	
	cout <<\n;"			 UPVC DOORFRAME HINGES			\n\n\n";	
	
	cout <<"enter the quantity\n";
	cin >>quantity;
	quantity=getchar();
	cout <<"enter height\n";
	cin >>height;
				ans=getans();	
	
	 
	cout<<"\n";   
	
			 
	for (bottom= 0; bottom<top; bottom=bottom+putchar()){	
		cout <<"please wait while machine dri hole\n";
		}  
	
	
	for (top = 0; top!=bottom; top=top+putchar()){
		cout <<"please wait while machine dril hole\n";}
	  
		
	for (center = 0; center==getchar()/2;center){
		cout <<"please wait while machine drill hole\n";}							 
						  
system ("pause");




return 0;

}


it only do the first for loop
now it all mess up

This post has been edited by Lt_Kernel: 20 November 2007 - 01:47 PM

Was This Post Helpful? 0
  • +
  • -

#13 Lt_Kernel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 05-September 07

Re: I/O stream problems

Posted 21 November 2007 - 02:02 PM

int main ()

{	char getchar();
	 getchar x;
	 getchar y;
	 
		  
	int center,fix,fix2,quantity,height,bottom,top;   
	
	center=height/2;
	fix = 141+41;
	fix2 = 135+12;
	string str ("please wait while machine dri hole\n");   
			
	cout <<"enter the quantity\n";
	cin >>quantity;
	do {x=getchar();
		   getchar (x);
		   }
	   
	cout <<"enter height\n";
	{x = getchar(cin<<quantity);}
	
	cout<<"\n";	 
				   
	for (bottom= 0,top=0,center=0; bottom<=putc(quantity)&&top<=putc(quantity)&&center<=putc(quantity); bottom++,top++,center++)
		if (bottom==fix2 && top==fix && center==height/2)
	{	
		cout <<str<<endl;
		}								 
				 
system ("pause");




return 0;

}


this is th change
please help in puting the quantity in the condition area of the loop
Was This Post Helpful? 0
  • +
  • -

#14 Lt_Kernel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 05-September 07

Re: I/O stream problems

Posted 23 November 2007 - 11:41 AM

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <ctype.h>


using namespace std;



int main ()

{	
	 char x;
	 char y;
	 
		  
	int center,quantity,height,bottom,top;   
	
	center=height/2;
	top = 141+41;
	bottom = 138+12;
	string z ("please wait while machine drills hole\n");   
	x=quantity;
	y=center;	
		 
	cout <<"enter the quantity\n";
	cin >>x;
	x=getchar();
	cin.get();
	cout <<"enter height\n";
	cin >>height;
	y=getchar();
	cin.get();
	
  for (bottom= 0,top=0,center=0; bottom<=putchar(x)&&top<=putchar(x)&&center<=putchar(x); bottom++,top++,center++)		   
		if (center==putchar(y)/2)
		   
	{	
				cout<<y;
		} 
		else {
			 cout<<"not centered\n"; }				  
			  
system("pause");

return 0;

}


the code works but not as expected
it loops much number of times than the quantity
please help with this problem
Was This Post Helpful? 0
  • +
  • -

#15 Lt_Kernel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 05-September 07

Re: I/O stream problems

Posted 26 November 2007 - 03:00 PM

#include <iostream>
#include <string>



using namespace std;

int main ()

{		 
	int center,quantity,height,bottom,top;   
	
	
	top = 141+41;
	bottom = 138+12;
	string z ("please wait while machine drills hole\n");   
	float y;
	  y=height / 2;   
	cout <<"enter the quantity\n";
	cin >>quantity;
		
	cout <<"enter height\n";
	cin >>y;
	cout <<y/2<<endl;
	
	
  for (bottom= 0,top=0,center=0; bottom<=quantity&&top<=quantity&&center<=quantity; bottom++,top++,center++)		   
		
		   if (center==y/2)
	{	
				cout<<"bottom" <<bottom<<endl;
				cout<<"top" <<top<<endl;
				cout<<"center"<<center<<endl;
						} 
				else {cout<<"problem";}		
			  
system("pause");

return 0;

}


i need to correct the top center and bottom
thats the problem again

This post has been edited by Lt_Kernel: 26 November 2007 - 03:02 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2