Sep 5, 2019 at 1:13am UTC
Having trouble converting a string to a float in main in this program. Using Visual Studio 2017. Any ideas how to proceed?
//header file: Rectangle.h
#ifndef RECTANGLE_H
#define RECTANGLE_H
class Rectangle{
private:
float length;
float width;
public:
Rectangle();
float calcPerimeter();
float calcArea();
void setLength(float length);
void setWidth(float width);
float getLength();
float getWidth();
void showData();
};
#endif
___________________________________________________
// implementation file: Rectangle.cpp
//
#include "Rectangle.h"
using namespace std;
#include<iostream>
Rectangle::Rectangle(){
this->length = 0;
this->width = 0;
}
float Rectangle::calcPerimeter(){
return 2*(this->length + this->width);
}
float Rectangle::calcArea(){
return this->length*this->width;
}
void Rectangle::setLength(float length){
this->length = length;
}
void Rectangle::setWidth(float width){
this->width = width;
}
float Rectangle::getLength(){
return this->length;
}
float Rectangle::getWidth(){
return this->width;
}
void Rectangle::showData(){
cout<<"length = "<<this->length;
cout<<", width = "<<this->width;
cout<<", perimeter = "<<this->calcPerimeter();
cout<<", area = "<<this->calcArea();
}
_______________________________________________________________
// driver file: driver.cpp
//
#include "Rectangle.h"
#include "Rectangle.cpp"
using namespace std;
//
int main(){
float len,wid;
Rectangle rect1; // instance of Rectangle (object)
string input = "";
//
while(true){
do{
cout<<endl<<"Enter length: "; cin>>input;
if(input=="Ctrl-Z"){
exit(0);
}else
len = stof(input); // string to float
}while(len<=0);
//
do{
cout<<endl<<"Enter width: "; cin>>input;
if(input=="Ctrl-Z"){
exit(0);
}else
wid = stof(input); // string to float
}while(wid<=0);
//
// setting the Rectangle properties
rect1.setLength(len);
rect1.setWidth(wid);
rect1.showData(); // display
}
return 0;
}
Last edited on Sep 5, 2019 at 1:14am UTC
Sep 5, 2019 at 1:26am UTC
OP's code, formatted and put into code blocks:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
// header file: Rectangle.h
#ifndef RECTANGLE_H
#define RECTANGLE_H
class Rectangle {
private :
float length;
float width;
public :
Rectangle();
float calcPerimeter();
float calcArea();
void setLength(float length);
void setWidth(float width);
float getLength();
float getWidth();
void showData();
};
#endif
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
// implementation file: Rectangle.cpp
//
#include "Rectangle.h"
using namespace std;
#include <iostream>
Rectangle::Rectangle() {
this ->length = 0;
this ->width = 0;
}
float Rectangle::calcPerimeter() { return 2 * (this ->length + this ->width); }
float Rectangle::calcArea() { return this ->length * this ->width; }
void Rectangle::setLength(float length) { this ->length = length; }
void Rectangle::setWidth(float width) { this ->width = width; }
float Rectangle::getLength() { return this ->length; }
float Rectangle::getWidth() { return this ->width; }
void Rectangle::showData() {
cout << "length = " << this ->length;
cout << ", width = " << this ->width;
cout << ", perimeter = " << this ->calcPerimeter();
cout << ", area = " << this ->calcArea();
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
// driver file: driver.cpp
//
#include "Rectangle.cpp"
#include "Rectangle.h"
using namespace std;
//
int main() {
float len, wid;
Rectangle rect1; // instance of Rectangle (object)
string input = "" ;
//
while (true ) {
do {
cout << endl << "Enter length: " ;
cin >> input;
if (input == "Ctrl-Z" ) {
exit(0);
} else
len = stof(input); // string to float
} while (len <= 0);
//
do {
cout << endl << "Enter width: " ;
cin >> input;
if (input == "Ctrl-Z" ) {
exit(0);
} else
wid = stof(input); // string to float
} while (wid <= 0);
//
// setting the Rectangle properties
rect1.setLength(len);
rect1.setWidth(wid);
rect1.showData(); // display
}
return 0;
}
You use std::stof and your code does compile for me (though you really shouldn't be #including cpp files). What issue are you having?
-Albatross
Last edited on Sep 5, 2019 at 1:27am UTC
Sep 5, 2019 at 2:37am UTC
I get the error, 'std:stof is unidentified' Also my "cin >>" no operator matches these operands
Sep 5, 2019 at 2:44am UTC
Ah. You may need to #include <string>
in your driver.cpp file.
-Albatross
Sep 5, 2019 at 5:25am UTC
That fixed the stof problem, but now I'm getting "cin" and "cout" identifiers are undefined. Just to clarify Rectangle.h should be in headers and Rectangle.cpp and Driver.cpp should be in source files correct?
Last edited on Sep 5, 2019 at 5:41am UTC
Sep 5, 2019 at 5:27am UTC
So you need
#include <iostream>
in driver.cpp as well.
Last edited on Sep 5, 2019 at 5:28am UTC
Sep 5, 2019 at 1:47pm UTC
That fixed the "cin/cout" problem but now im getting errors saying all my public statement in Rectangle.h are already defined in Rectangle.obj
Last edited on Sep 5, 2019 at 1:48pm UTC
Sep 5, 2019 at 2:11pm UTC
Maybe it's time that you posted your up-to-date code. In CODE TAGS.
You don't need
#include "Rectangle.cpp"
in driver.cpp; the prototypes are pulled in by the #include "Rectangle.h"
statement.
Last edited on Sep 5, 2019 at 2:14pm UTC
Sep 5, 2019 at 3:00pm UTC
while (true );
Look VERY closely. Where does that loop end? And HOW DO YOU GET OUT OF IT?
Sep 5, 2019 at 3:07pm UTC
doesn't
rect1.showData();
end the loop?
Sep 5, 2019 at 3:11pm UTC
No, the
;
at the end of the while
statement bounds the loop. And since the content of the while
test is forever true
... it will repeat that line for ever!
Just remove that semicolon and leave
while (true )
Last edited on Sep 5, 2019 at 3:15pm UTC
Sep 5, 2019 at 3:23pm UTC
Wow can't believe I missed that... I just need to add some error checking and I'm done. Appreciate all the help!
Sep 5, 2019 at 3:35pm UTC
if (input == "Ctrl-Z" )
I don't think this does what you expect. It will only be true of the user types the 6 characters "C" "t" "r" "l" "-" "Z" and presses Enter. I think what you're looking for is whether they held down the CTRL key and pressed Z.
That combination (on Windows) tells the OS that the stream has ended. To test for this and other error conditions, replace if (input == "Ctrl-Z" )
with if (!cin)
Sep 5, 2019 at 3:40pm UTC
on windows, in ascii modes, ctrlz is 26 (ascii code). But i am not sure you can actually 'read' it from cin. Its not printable...
Sep 5, 2019 at 9:52pm UTC
Fixed main to accept "Ctrl-Z" to quit. Only thing I need now is error checking for not accepting letters as input.