C++ String variables

This is a discussion on C++ String variables within the C++ Programming forums, part of the General Programming Boards category; I'm trying to make variables to hold strings of text and am getting the following errors: Line 14: 'byte' undeclared ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Location
    North Carolina
    Posts
    2

    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.

  2. #2
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269
    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()
    Last edited by SirCrono6; 01-15-2006 at 09:50 PM.
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  3. #3
    adr
    adr is offline
    Registered User
    Join Date
    Dec 2005
    Posts
    155
    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.
    Last edited by adr; 01-15-2006 at 10:17 PM.

  4. #4
    xhi
    xhi is offline
    !anExpert
    Join Date
    Mar 2005
    Location
    pa
    Posts
    155
    also as SirCrono6 suggested, lose the .h on your header files..
    ex

    #include <iostream>
    #include <string>

    >> start:

    are you using goto?

  5. #5
    Registered User
    Join Date
    Jan 2006
    Location
    North Carolina
    Posts
    2
    Thanks a lot, removing the ".h" from the header files worked, and yes I'm using goto.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using SSCANF input string output two variables
    By kevndale79 in forum C Programming
    Replies: 9
    Last Post: 10-02-2006, 02:57 PM
  2. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  4. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM
  5. "Operator must be a member function..." (Error)
    By Magos in forum C++ Programming
    Replies: 16
    Last Post: 10-28-2002, 01:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21