Newbie Mistake?

This is a discussion on Newbie Mistake? within the C++ Programming forums, part of the General Programming Boards category; Seems that I can not get this code to work correctly. I've looked over it many times but forever whatever ...

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    10

    Newbie Mistake?

    Seems that I can not get this code to work correctly. I've looked over it many times but forever whatever reason. I'm still unable to get this code to run all the way though and output the last part of the code. It seems to happen to everyone of my simple little programs I make. Can someone please assist me, and tell me what I am doing wrong?

    Code:
     
    // Simple program to figure out the size of a volume of a box...
    
    #include <vcl.h>
    #include <iostream>
    #pragma hdrstop
    #pragma argsused
    using namespace std;
    
    int main() {
    
    	double side1, side2, square;
    
    	cout << "Enter the frist side of the square: ";
    	cin >> side1;
    	cout << "Now enter side 2: ";
    	cin >> side2;
    
    	square = (side1 * side2);
    
    	cout << "Square total is: " << square;
    
    	return 0;
    }

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    It looks ok to me. Are you sure its not just exiting before you get to see the output? Maybe try putting: cin.ignore(); before your return 0.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,975
    How does it not work? Are you running it from the command line? You might want to remove the #include <vcl.h> as you do not need it, and the pragmas probably are not necessary too.

    By the way, it looks like you want to calculate the area of a rectangle, since the length of the sides of a square are the same
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Jul 2006
    Posts
    10
    Well, it seems that I am unable to get that to work either, tried putting in cin.ignore(); ... However that still didnt work, it seems that the Prompt is just closing out befor i see what the output is, the answer to your question.

    *Cries*

  5. #5
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Okay, well maybe you could replace cin.ignore() with cin >> side1; and see if that works?

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ok, so every time you do "cin >> something;" you need to follow it by "cin.ignore();", and then finish your application with "cin.get();" to make it wait for a key-press.

    Or just run your application in a command prompt in the first place.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered Abuser
    Join Date
    Sep 2007
    Location
    USA/NJ/TRENTON
    Posts
    127
    might the #pragma hdrstop be keeping the changes from being saved?

  8. #8
    Registered User
    Join Date
    Jul 2006
    Posts
    10
    Looks like adding the cin.get(); cin.ignore(); took care of the issue all together... Unsure what the "#pragma hdrstop" is really for to be honest. I tried taking it outta the code and the C++ Builder I am using just gives me an error so I am not sure if that is the reason i get an error or what.


    Ahh who knows, i am just happy now that I am finally able to see the end result of my program.

    Thanks everyone!

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    #pragma hdrstop is for precompiled headers. You really don't need that except to speed up the compile time of very large projects.

    #pragma argsused probably affects the warnings BC++ emits.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    Registered User
    Join Date
    Sep 2007
    Posts
    12
    u need to put cin.ignore(2); and then you will be able to see the results before the complier shuts it down. put it before the return 0; and it should work. and if you know you wont be using decimals then use int instead.

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >u need to put cin.ignore(2); and then you will be able to see the results before the complier shuts it down.
    Unless there are 3 extra characters. Or 4. Or 5. Where do you want to stop? It's better to avoid this problem entirely by using std::getline and std::string for all of your input needs and then converting using something like std::stringstream or boost::lexical_cast<>. However, if you hit this problem, the conventional solution is this:
    Code:
    #include <iostream>
    #include <ios> // For streamsize
    #include <limits> // For numeric_limits<>
    
    int main()
    {
      // ...
    
      std::cin.clear();
      std::cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );
      std::cin.get();
    }
    These three lines clear the stream state so that you can read from the stream (if the stream is in a failure state, any input requests will fail), then remove all of the characters in the stream up to a newline, and finally make a blocking read. If there are extraneous characters in the stream, this will pause the program until you type a newline.

    If the stream is empty, you'll end up having to type a newline twice because both cin.ignore and cin.get are blocking reads. You can fix that (if it's really an issue) with a little stream buffer magic trick:
    Code:
    #include <iostream>
    #include <ios> // For streamsize
    #include <istream> // For basic_istream<>
    #include <limits> // For numeric_limits<>
    
    namespace jsw {
      template <typename CharT, typename Traits>
      std::basic_istream<CharT, Traits>& ignore (
        std::basic_istream<CharT, Traits>& in, CharT delim )
      {
        if ( in.rdbuf()->sungetc() != Traits::eof() && in.get() != delim )
          in.ignore ( std::numeric_limits<std::streamsize>::max(), delim );
    
        return in;
      }
    }
    
    int main()
    {
      // ...
    
      jsw::ignore ( std::cin, '\n' );
      std::cin.clear();
      std::cin.get();
    }
    How it works really isn't important at this point because if you're having trouble with basic I/O, throwing you head first into stream buffers probably isn't a good idea.
    My best code is written with the delete key.

  12. #12
    Registered User
    Join Date
    Sep 2007
    Posts
    12
    all you need to declare in this program is <iostream> and using namespace std;

    and to stop the program from exiting before your ready use cin.ignore(2);
    like this
    Code:
      cin.ignore(2);
      return 0;
    } //end of int main or w/e

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >and to stop the program from exiting before your ready use cin.ignore(2);
    Did you even bother to read my post?
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newbie: array question :(
    By cstudent in forum C Programming
    Replies: 2
    Last Post: 04-09-2008, 06:46 AM
  2. Please help! - Who find mistake in the program??
    By nivek in forum C++ Programming
    Replies: 4
    Last Post: 01-06-2008, 12:28 AM
  3. Please assistance, general protection mistake
    By louis_mine in forum C++ Programming
    Replies: 3
    Last Post: 10-23-2004, 10:45 PM
  4. Whom do you blame for the mistake in Pres. Bush's State of the Union speech?
    By zahid in forum A Brief History of Cprogramming.com
    Replies: 20
    Last Post: 07-15-2003, 07:03 AM
  5. Newbie Game Develpoers Unite!
    By Telenosis in forum Game Programming
    Replies: 10
    Last Post: 06-22-2002, 02:02 PM

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