C++ Beginner Help On XP

This is a discussion on C++ Beginner Help On XP within the C++ Programming forums, part of the General Programming Boards category; I searched the forum and help, and couldn't find anything specific enough, that is why I made this thread. So ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    8

    C++ Beginner Help On XP

    I searched the forum and help, and couldn't find anything specific enough, that is why I made this thread. So I am currently using Dev-C++ 4.9.9.2 as my preferred compiler. I was reading the beginner section of this website and was reading this code:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      int thisisanumber;
    
      cout<<"Please enter a number: ";
      cin>> thisisanumber;
      cin.ignore();
      cout<<"You entered: "<< thisisanumber <<"\n";
      cin.get();
    }
    So, I changed a few things:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        char word;
        
        cout<<"Please type a word and I will show it back to you:";
        cin>> word;
        cin.ignore();
        cout<<"You entered: "<< word <<"\n";
        cin.ignore();
        cin.get();
    }
    The problem with my code is that I can type in a word but, when I hit enter, it displays the word for a brief flash and then the Dos box closes. If anyone can point me in the right direction with a link or advice, that would be much appreciated. By the way, I am completely new to programming and this website.

    -Paul
    Last edited by rayne117; 01-04-2009 at 09:22 PM.

  2. #2
    verbose cat
    Join Date
    Jun 2003
    Posts
    209
    See the FAQ specifically this
    abachler: "A great programmer never stops optimizing a piece of code until it consists of nothing but preprocessor directives and comments "

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    8
    I think I will finish up all the beginner lessons, seeing as how I don't understand anything of either of those links. Anyways, thanks for the help.

  4. #4
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    And you can't store a word in a char. A char is just a single character. For a word you need a std::string.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        string word;
        
        cout<<"Please type a word and I will show it back to you:";
        cin>> word;
        cin.ignore();
        cout<<"You entered: "<< word <<"\n";
        cin.ignore();
        cin.get();
    }

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,900
    The problem is that it reads only a character, not the whole word. Thus it would just read another character instead of halting the execution and waiting for input.

    But again, it is not necessary to do so. There are 3 techniques to avoid it.
    1) Use an IDE that forces the window to remain open after execution ends (Visual Studio can do it, Code::Blocks is supposed to have a feature, too).
    2) Use a debugger and put a breakpoint at the closing } to hinder the program from ending. Visual Studio has a debugger, and is probably the easiest one to do it with.
    3) Run it from the command prompt.

    And it is better to use std::getline instead of cin >>:
    Code:
    #include <iostream>
    
    int main()
    {
        std::string word;
        
        std::cout<<"Please type a word and I will show it back to you:";
        std::getline(std::cin, word);
        std::cout<<"You entered: "<< word <<"\n";
    }
    Because otherwise it will refuse to read things with spaces. Just be aware of that.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    1

    answer

    you done a realy hard work this code is very simple
    first :
    you can not save a string in a char you must say
    char word[100]
    it means that string is a group of charecters save in an array
    second
    it is normal if your program finish and end cause you done nothing to tell program stop before terminating i have some ways i can tell you
    first compile your program with Ctrl+F5 but even now if you open yourprogram.exe it will terminate suddenly okay to solve this problem you have these ways add one of the below codes at the end of your program

    1. getch(); //this need conio.h program will wait untill you press a key
    2.system("pause"); // this need iostream.h or in dotNet iostream with using namespace std this will call "pause" command from shell you will see this statement "please press anykey to continue . . . "
    3.Sleep(time in milisecond); //this one need Windows.h and program will wait as much as you enter between "()"

    Code:
    #include <iostream>
    #include <conio.h>
    #include <Windows.h>
    
    using namespace std;
    
    int main()
    {
        char word[100];
        
        cout<<"Please type a word and I will show it back to you:";
        gets(word);
        cout<<"You entered: ";
        puts(word);
        getch(); // or system ("pause"); or Sleep(500);
    
    }
    attention Sleep(); is one of windows API's functions string word also is true
    aslo note i told you easier way of writing that program but yes as i include more headers my program size will be more than yours

  7. #7
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479
    or get rid of system("pause"); and use cin.get();
    I'm just trying to be a better person - My Name Is Earl

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,900
    farbod69, it is fine that you want to help, but please, do not use such a poor way of demonstrating it. First off, gets is a C function! Secondly, it is extremely bad practice. http://cpwiki.sourceforge.net/Gets
    Secondly, puts is also a C function.
    Thirdly, in C++, it is better to use std::string instead of char.
    Fourthly, getch is a bad way of making the console pause, as is system("pause"). Some ways are described here http://apps.sourceforge.net/mediawik...=Pause_console.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 05-07-2009, 12:31 PM
  2. Trying to Install XP over Vista with SATA HD
    By Shamino in forum Tech Board
    Replies: 2
    Last Post: 12-13-2008, 06:56 PM
  3. Need help with program
    By HAssan in forum C Programming
    Replies: 8
    Last Post: 06-10-2007, 09:05 PM
  4. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM
  5. Windows XP regression over time
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 12-17-2002, 10:49 AM

Tags for this Thread


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