hey ..

how are u all ??

I have c++ problem .. i tried to solve it but i couldnt get the same input ..

here is the problem:

Given the following file called “input.txt” which contains the freezing and boiling points of several substances. Note: the title is not part of the data.

Substance Freezing Point Boiling Point (Celsius)
Ethyl 78 -114
Mercury 358 -38
Oxygen -188 -219
Water 100 0

Write a program that asks the user to enter a temperature, and then shows all the substances that will freeze at that temperature and all the substances that will boil at that temperature.
For example, if the user enters -30 the program should report that water will freeze and oxygen will boil at that temperature.

Input/Output Sample:

Enter the temperature: -30
Water freezes and Oxygen boils

we should use if-else to do it ..

They have mentioned everything so clearly... Use if else or if else ladder to find the ranges in which the given temperature falls and print accordingly...

i tried to solve it but i couldnt get the same input ..

thats what I did ..

``````string s1,s2,s3,s4;

int f1,b1,f2,b2,f3,b3,f4,b4;
int t;

ifstream infile;

infile.open("input.txt");
infile>>s1>>f1>>b1>>s2>>f2>>b2>>s3>>f3>>b3>>s4>>f4>>b4;

cout<<"\n";

cout<<"  Enter the temperature: ";
cin>>t;

cout<<"\n";

if(t<=f1)
cout<<s1<<" freezes ";

else if(t>=b1)
cout<<s1<<" boils  ";

if(t<=f2)
cout<<s2<<" freezes ";

else if(t>=b2)
cout<<s2<<" boils  ";
.
.
.
.

etc .....``````

s1 means substance 1 .. f means freezing point .... b means boiling ..

when i run the program i get an input like ..

Enter the temperature: -30
Oxygen boils Water freezes

i dont know where to put and .. and why the water freezing comes after the oxygen??

how to make it like this ::

Enter the temperature: -30
Water freezes and Oxygen boils

I think your approach is better if you add line breaks, but you can probably get the right output like this:

``````open file
let n be 1

while more substances

if substance freezes
print name + " freezes"
else if substance boils
print name + " boils"
else
continue

if n++ is even
print line break
else
print " and "
end while

close file``````

I cant use while ...

i should do it with if -else only ..

any suggestions ??

So you have two problems: you don't know where to insert the "and" so that it appears in the output, and you want water to appear first.

The first problem is simple: your "and" can be tagged on to the end of the s1 cout statement, which will flow into your s2 statement (like it does now) giving you a complete sentence like "s1 boils and s2 freezes.

The second problem is that you have s1 defined as oxygen and s2 as water. If you want water to show up first in your output, then you need to switch s1 to the second if/ else statement, or go into the text file and change the assigned values so that s1 is defined as water and s2 is oxygen. If you took the second suggestion, you could leave the c++ code as is. Otherwise, change s1 to s2 and s2 to s1. Then oxygen will be second to water.

``````if(t<=f1)
{
cout<<s1<<" freezes and ";
}
else if(t>=b1)
{
cout<<s1<<" boils and ";
}
if(t<=f2)
{
cout<<s2<<" freezes ";
}
else if(t>=b2)
{
cout<<s2<<" boils ";
}
.``````

I cant use while ...

i should do it with if -else only ..

any suggestions ??

If the number of substances in the file is fixed, you can unroll the loop because the logic inside the loop is what you want for getting the right output. The loop itself just makes the code shorter and easier to follow.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.