-
C++ String variables
I'm trying to make variables to hold strings of text and am getting the following errors:
Line 14: 'byte' undeclared (first use this function)
Line 14: (Each undeclared identifier is reported only once
Line 14: for each function it appears in.)
Line 18: 'byte2' undeclared (first use this function)
Here's my code:
Code:
#include <iostream.h>
#include <string.h>
using namespace std;
int main()
{
string byte, byte2;
char answer;
int size;
cout<<"This program is design to convert computer memory sizes.\n";
start:
cout<<"What is the base byte? (b,kb,mb,gb,tb) ";
cin>> byte;
cout<<"How many of the base byte? ";
cin>> size;
cout<<"What would you like to convert it to? ";
cin>> byte2;
if ( byte == "b" ) {
if ( byte2 == "kb" ) {
cout<<"There are "<< (size/1024) <<"kilobytes in "<< size <<"bytes\n";
}
if ( byte2 == "mb" ) {
cout<<"There are "<< ((size/1024)/1024) <<"megabytes in "<< size <<"bytes\n";
}
if ( byte2 == "gb" ) {
cout<<"There are "<< (((size/1024)/1024)/1024) <<"gigabytes in "<< size <<"bytes\n";
}
if ( byte2 == "tb" ) {
cout<<"There are "<< ((((size/1024)/1024)/1024)/1024) <<"terabytes in "<< size <<"bytes\n";
}
}
There's more to the program but I'm only posting what I think is pertinent to the problem at hand and for the sake of space.
Thanks.
-
Code:
#include <iostream> //Drop .h
#include <string> //Drop .h
using namespace std;
int main()
{
string byte, byte2;
char answer;
int size;
cout<<"This program is design to convert computer memory sizes.\n";
start: //What is this for?
cout<<"What is the base byte? (b,kb,mb,gb,tb) ";
cin>> byte;
cout<<"How many of the base byte? ";
cin>> size;
cout<<"What would you like to convert it to? ";
cin>> byte2;
if ( byte == "b" ) {
if ( byte2 == "kb" ) {
cout<<"There are "<< (size/1024) <<"kilobytes in "<< size <<"bytes\n";
}
if ( byte2 == "mb" ) {
cout<<"There are "<< ((size/1024)/1024) <<"megabytes in "<< size <<"bytes\n";
}
if ( byte2 == "gb" ) {
cout<<"There are "<< (((size/1024)/1024)/1024) <<"gigabytes in "<< size <<"bytes\n";
}
if ( byte2 == "tb" ) {
cout<<"There are "<< ((((size/1024)/1024)/1024)/1024) <<"terabytes in "<< size <<"bytes\n";
}
}
} //Need a closing brace for main()
-
Will 1st off he said there was more to his code. So dont worry about closing his "int main()" just yet. Ok Will I know something you could change around 1st. I put them into your code.
Code:
#include <iostream.h>
#include <string.h>
using namespace std;
int main()
{
string byte;
string byte2;
char answer;
int size;
cout<<"This program is design to convert computer memory sizes.\n";
cout<<"What is the base byte? (b,kb,mb,gb,tb) ";
getline(cin, byte);
cout<<"How many of the base byte? ";
getline(cin, size);
cout<<"What would you like to convert it to? ";
getline(cin, byte2);
if ( byte == "b" ) { //<~~this right here needs a cout also. Something like.
cout<<"There are"<<(size/size)<<"something in"<<size <<"bytes\n";
}
else if ( byte2 == "kb" ) {
cout<<"There are "<< (size/1024) <<"kilobytes in "<< size <<"bytes\n";
}
else if ( byte2 == "mb" ) {
cout<<"There are "<< ((size/1024)/1024) <<"megabytes in "<< size <<"bytes\n";
}
else if ( byte2 == "gb" ) {
cout<<"There are "<< (((size/1024)/1024)/1024) <<"gigabytes in "<< size <<"bytes\n";
}
else if ( byte2 == "tb" ) {
cout<<"There are "<< ((((size/1024)/1024)/1024)/1024) <<"terabytes in "<< size <<"bytes\n";
}
I hope that helps some what. Good luck.
-
also as SirCrono6 suggested, lose the .h on your header files..
ex
#include <iostream>
#include <string>
>> start:
are you using goto?
-
Thanks a lot, removing the ".h" from the header files worked, and yes I'm using goto.