1

Here is my code :

#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <string.h>

main()
{
      char a[120];
      int b;

      cout<<"age : ";
      cin>>b;

      cout<<"Name : ";
      gets(a);

      cout<<"Name : "<<a<<endl;
      cout<<"age  : "<<b;
      cout<<endl;

      system("pause");
      return 0;
}

output : age : 20 // I type 20 Name: Name : // it doesn't ask to type name. age : 20 press any key to continue ...

But, if I use gets(a) first, it's ok.

#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <string.h>

main()
{
      char a[120];
      int b;

      cout<<"Name : ";
      gets(a);

      cout<<"age : ";
      cin>>b;

      cout<<"Name : "<<a<<endl;
      cout<<"age  : "<<b;
      cout<<endl;

      system("pause");
      return 0;
}

output : Name : John // I type John. age : 20 // I type 20.

Name : John age : 20 Press any key to continue...

What happen to them ... Please Help ...

milleniumbug
  • 15,379
  • 3
  • 47
  • 71
Kelvin Thomas
  • 13
  • 1
  • 3
  • Note: The C++ headers are called , , and . – typ1232 May 31 '13 at 10:56
  • @typ1232 You meant and – milleniumbug May 31 '13 at 11:24
  • 2
    Never ever use `gets`! This is the one function that is so incredibly broken by design that it was actually **removed** from newer versions of the C standard. There is no way to use it safely. If you have to use C I/O, use `fgets(stdin, ...)`. – Sebastian Redl May 31 '13 at 11:51
  • Update your environment and at least use [Orwell Dev-C++](http://sourceforge.net/projects/orwelldevcpp/), which uses a modern C++ compiler and MinGW-w64. Or use a better IDE. There are tons around. – rubenvb May 31 '13 at 12:18
  • Also: Don't use `system("pause")`. Make your IDE hold the console open or run your program directly in a console window. – rubenvb May 31 '13 at 12:19

2 Answers2

5

Whatever book/internet tutorial you're learning from right now, throw it away. No, seriously. This code reads as if it was written in 1992. Here is the list of books recommended by Stack Overflow community.

Also, if your Dev-C++ version is 4.9.9.2, I highly recommend an update - newer IDE will provide better editor, better debugger, and, (really important) better compiler. Better compiler will give you better error messages, and that is important when learning C++ (trust me on that, I know from experience) Common alternatives are: Visual C++, Code::Blocks or if you like Dev-C++, just upgrade to Orwell Dev-C++.

Let us fast forward to 2013 now.

Warnings

There are several jokes that can be reduced to "meh, i listen to errors not to warnings". They are wrong. 95% of warnings mean that the code is wrong. Since C++ is a tricky language, everyone should enable warnings in theirs IDEs (puzzles me why they are not enabled by default).

Headers

Introductions of namespaces to C++ means old headers are deprecated. This means they should not be used when writing new code.

The headers names are changed like this:

  • builtin C++ headers don't have ".h" at the end. ("iostream.h" --> "iostream")

  • Headers originated from C don't have ".h" at the end, also they are prefixed with "c" ("stdio.h" --> "cstdio")

  • conio.h is neither builtin C header nor builtin C++ header, so the name stays like it was.

Here is list of standard headers

Strings

C++ has a string type. std::string. It allows for simple string operations without hassle. Just #include <string> to use it.

string first_name = "John";
string surname = "Titor";
string full_name = first_name + " " + surname; //easy concatenation

//string a = "first" + " " + "name" //nasty surprise

string c = "first" + string(" ") + "name" //much better
//or using stringstream (#include <sstream>)
stringstream ss;
ss << "first" << " " << "name";
c = ss.str();

gets()

This function is impossible to use safely - it doesn't limit user input nor expands buffer. In your example code, someone could enter more than 120 characters and end up envoking undefined behaviour. This term is equivalent to math's undefined behaviour - anything can happen, including 1 being equal to 2. The gets() function was recently removed from C language altogether. C++ has a safe alternative.

string s;
getline(cin, s);

Proper declaration of main()

int main()

or

int main(int argc, char* argv[])
Community
  • 1
  • 1
milleniumbug
  • 15,379
  • 3
  • 47
  • 71
  • [Here's a comprehensive list of C++ Standard Library headers.](http://en.cppreference.com/w/cpp/header) Other than that, +1. Also, I suggest using clang++, it features the most legible and useful messages IMO. – dyp May 31 '13 at 12:47
  • @DyP Your list better explains what each header is for. I'll add it in to my answer. – milleniumbug May 31 '13 at 12:51
0

Avoid use functions from iostream and stdio together.

However, your problem is caused by an additional Enter.

When you enters age and press enter, cin only comsumes the number. The Enter is passed to gets() so it directly returns.

A fix could be

#include<iostream>
#include<stdio.h>
#include<string>

using namespace std;

main()
{
    char a[120];
    int b;

    cout<<"age : ";
    cin>>b;

    cin.ignore();  //add this line to eat the enter

    cout<<"Name : "<<flush;
    gets(a);

    cout<<"Name : "<<a<<endl;
    cout<<"age  : "<<b;
    cout<<endl;

    system("pause");
    return 0;
}
Naruil
  • 2,300
  • 11
  • 15
  • Thank you very much !It really works. But, I still don't understand why you add "flush". It still works without flush . I was a little bit confuse . Could you explain me? Thank you. – Kelvin Thomas May 31 '13 at 11:35
  • Just to make sure the "Name :" thing is printed out before `get(a);`. In this case it is useless so you can remove it. – Naruil May 31 '13 at 11:43