-
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
-
-
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.
-
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();
}
-
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.
-
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
-
or get rid of system("pause"); and use cin.get();
-
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.