Someone tell me what I'm missing here?

This is a discussion on Someone tell me what I'm missing here? within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> #include <iomanip> using namespace std; const double RED_RUG = 4.99; const double GREEN_RUG = 5.99; const double ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    33

    Someone tell me what I'm missing here?

    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    
    const double RED_RUG = 4.99;
    const double GREEN_RUG = 5.99;
    const double BLUE_RUG = 6.99;
    
    
    
    
    
    
    int main()
    
    {
    	double customerName;
    	double rugColor;
        double paymentMethod;
    
    	cout<<setw(20)<<"Welcome to..."<<endl;
    	cout<<endl;
    	cout<<setw(40)<<">>>Toupay's Warehouse<<<<"<<endl;
    	cout<<endl;
    	cout<<setw(35)<<"The Best in rugs"<<endl;
    	cout<<endl;
    	cout<<"If you enter the customers name, type of rug purchase,"<<endl;
        cout<<"room dimesions, and type of payment, this program"<<endl;
        cout<<"will calculate and print a bill to the screen"<<endl;
    
    
    	cout<<"Please enter the customers name: "<<endl;
    	cin>>customerName;
    
    	cout<<"Please enter rug color (R)ed (B)lue (G)reen: "<<endl;
        cin>>rugColor;
    	cout<<endl;
    
        cout<<"Please enter the method of payment:"<<endl;
    	cout<<"(C)ash (Z)ippy or (O)ther"<<endl;
    	cin>>paymentMethod;
        cout<<endl;
    	
    
    
    	
    
    
    
    	return 0;
    
    }
    it's not stopping for user input after customer name and rug color...any ideas? Thanks in advance.

  2. #2
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    your data types are all wrong and its a bad idea to input strings using cin, use cin.getline:
    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    
    const double RED_RUG = 4.99;
    const double GREEN_RUG = 5.99;
    const double BLUE_RUG = 6.99;
    
    
    int main()
    
    {
    	char customerName[25];
    	char rugColor;
    	char paymentMethod;
    
    	cout<<setw(20)<<"Welcome to..."<<endl;
    	cout<<endl;
    	cout<<setw(40)<<">>>Toupay's Warehouse<<<<"<<endl;
    	cout<<endl;
    	cout<<setw(35)<<"The Best in rugs"<<endl;
    	cout<<endl;
    	cout<<"If you enter the customers name, type of rug purchase,"<<endl;
    	cout<<"room dimesions, and type of payment, this program"<<endl;
    	cout<<"will calculate and print a bill to the screen"<<endl;
    
    
    	cout<<"Please enter the customers name: "<<endl;
    	cin.getline(customerName,25);
    
    	cout<<"Please enter rug color (R)ed (B)lue (G)reen: "<<endl;
    	cin>>rugColor;
    	cout<<endl;
    
    	cout<<"Please enter the method of payment:"<<endl;
    	cout<<"(C)ash (Z)ippy or (O)ther"<<endl;
    	cin>>paymentMethod;
    	cout<<endl;
    	
    	return 0;
    
    }
    -LC
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    33
    Thanks Lynux-Penguin that fixed it B)>

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    33
    ran into another problem
    I'm trying to input a charctor into a variable then output it in another place but I am getting errors...ideas?
    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    
    const double RED_RUG = 4.99;
    const double GREEN_RUG = 5.99;
    const double BLUE_RUG = 6.99;
    
    
    
    
    
    
    int main()
    
    {
    	char customerName[25];
    	char rugColor;
        char paymentMethod;
    	int roomLength;
        int roomWidth;
    	char rugDisplay;
    
    	cout<<setw(20)<<"Welcome to..."<<endl;
    	cout<<endl;
    	cout<<setw(40)<<">>>Toupay's Warehouse<<<<"<<endl;
    	cout<<endl;
    	cout<<setw(35)<<"The Best in rugs"<<endl;
    	cout<<endl;
    	cout<<"If you enter the customers name, type of rug purchase,"<<endl;
                    cout<<"room dimesions, and type of payment, this program"<<endl;
                    cout<<"will calculate and print a bill to the screen"<<endl;
    
    
    	cout<<"Please enter the customers name: ";
    	cin.getline(customerName,25);
    
    	cout<<endl;
    
    	cout<<"Please enter rug color (R)ed (B)lue (G)reen: ";
                    cin>>rugColor;
    ***trying to input here****
    	if (rugColor == R)
    	{
    		rugDisplay == RED
    	}
    	else if (rugColor == B)
    	{
    		rugDisplay == BLUE
    	}
    	else if (rugColor == G)
    	{
    		rugDisplay == GREEN
    	}
    
    	cout<<endl;
    
                    cout<<"Please enter the method of payment"<<endl;
    	cout<<"(C)ash (Z)ippy or (O)ther: ";
    	cin>>paymentMethod;
        
    	cout<<endl;
    
    	cout<<"Enter the Length of your room: ";
    	cin>>roomLength;
    
    	cout<<endl;
    
    	cout<<"Enter the Width of your room: ";
    	cin>>roomWidth;
    
    	cout<<endl;
    
    	cout<<setw(40)<<">>>Toupay's Warehouse<<<<"<<endl;
    	cout<<endl;
    	cout<<setw(35)<<"The Best in rugs"<<endl;
    
    	cout<<"Customer Name:"<<setw(25)<<customerName<<endl;
    
    	cout<<endl;
    
    	cout<<"Rug Color:"<<setw(25)<<rugColor<<endl;
    
    
    
    	
    	return 0;

  5. #5
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    
    const double RED_RUG = 4.99;
    const double GREEN_RUG = 5.99;
    const double BLUE_RUG = 6.99;
    
    int main()
    
    {
    	char customerName[25];
    	char rugColor;
        char paymentMethod;
    	int roomLength;
        int roomWidth;
    	char* rugDisplay; //changed char to char*
    
    	cout<<setw(20)<<"Welcome to..."<<endl;
    	cout<<endl;
    	cout<<setw(40)<<">>>Toupay's Warehouse<<<<"<<endl;
    	cout<<endl;
    	cout<<setw(35)<<"The Best in rugs"<<endl;
    	cout<<endl;
    	cout<<"If you enter the customers name, type of rug purchase,"<<endl;
                    cout<<"room dimesions, and type of payment, this program"<<endl;
                    cout<<"will calculate and print a bill to the screen"<<endl;
    
    
    	cout<<"Please enter the customers name: ";
    	cin.getline(customerName,25);
    
    	cout<<endl;
    
    	cout<<"Please enter rug color (R)ed (B)lue (G)reen: ";
                    cin>>rugColor;
    /***trying to input here****/
    	if (rugColor == 'R')//I changed R to 'R' etc.
    	{
    		rugDisplay = "RED"; //changed == to =
    	}
    	else if (rugColor == 'B')
    	{
    		rugDisplay = "BLUE";
    	}
    	else if (rugColor == 'G')
    	{
    		rugDisplay = "GREEN";
    	}
    	else
    		rugDisplay = "N/A"; //added an else clause
    	cout<<endl;
    
                    cout<<"Please enter the method of payment"<<endl;
    	cout<<"(C)ash (Z)ippy or (O)ther: ";
    	cin>>paymentMethod;
        
    	cout<<endl;
    
    	cout<<"Enter the Length of your room: ";
    	cin>>roomLength;
    
    	cout<<endl;
    
    	cout<<"Enter the Width of your room: ";
    	cin>>roomWidth;
    
    	cout<<endl;
    
    	cout<<setw(40)<<">>>Toupay's Warehouse<<<<"<<endl;
    	cout<<endl;
    	cout<<setw(35)<<"The Best in rugs"<<endl;
    
    	cout<<"Customer Name:"<<setw(25)<<customerName<<endl;
    
    	cout<<endl;
    
    	cout<<"Rug Color:"<<setw(25)<<rugColor<<endl;
    
    
    
    	
    	return 0;
    }
    It's up to you to determine why I made these changes, if you need any help let me know.

    -LC
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  6. #6
    Registered User
    Join Date
    Sep 2003
    Posts
    33
    Thanks again man, I understand why the quotes were changed as I had the wrong ones in there but I don't fully understand why I needed a char*. There a page you know of wehre I can read up on it?

  7. #7
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Furious, a char is one single character. E.g. a character variable could be 'A' or 'v' but it could never be "hi".

    A string is an array of characters, and one thing you can do is have a pointer to an array. A pointer basically is a memory address where something lives -- in this case, the pointer tells where the first element of the string is located in memory.

    A char * often is used to point to an array that holds string data. In this case, this line:

    rugDisplay = "Red";

    causes the compiler to:

    1) Create a string containing "Red\0" in a data section (this string is often read-only).
    2) When this line is executed, the variable rugDisplay gets set to the address where the string begins (the address of the 'R' in "Red".

    When a string is printed, the system will output all characters, beginning with the one pointed to, until the null character ('\0') is reached. So, when this string is printed, the characters 'R', 'e', and 'd' are printed, then the null termination is detected and printing stops. You do not need to explicitly add the '\0' character in this case -- a string literal like "Red" implicitly has null termination. "Red" also causes the compiler to allocate room for 4 characters, not 3.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  8. #8
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    Indeed, good job Cat

    Another thing...
    use single quotes (') for single characters
    [code]
    char b = 'b';
    [code]

    use (") for strings
    Code:
    char* str = "my String";
    Also just a side note (may be out of subject).
    Double quoted strings like the above are known as literal strings, they are null terminated strings but they are not CREATED they are permenantly stored in a special part of your program called the data section (or is it text). And in your program, where you wrote that literal string it replaces it with the pointer to the string in the data section of the program.

    and the reason we can use char* is because it is a pointer and the double quoted string gets replaced with a pointer to the string in the data section of memory where your program is loaded.

    -Kotatsu
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  2. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 02:49 PM
  3. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  4. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  5. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 03:49 AM

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