-5

There is something wrong with I/O of the following code.Just after I enter the t, i get an output line, which I can not account for, I am a beginner, and this is so frustrating. Please have a look.

#include<bits/stdc++.h>

using namespace std;

int main()
{
    string name;
    string sname;
    int t;
    cin>>t;
while(t--)
{

    getline(cin,name);
    *(name.begin())-=32;
    if(name.find(" ")==-1)
    {
        cout<<name;
    }

    *(name.begin()+name.find(" "))=49;
    if(name.find(" ")==-1)
    {
            *(name.begin()+name.find("1")+1)-=32;
            sname=name.substr(name.find("1")+1);
            cout<<*(name.begin())<<"."<<" "<<sname;
    }
    else
    {
        *(name.begin()+name.find("1")+1)-=32;
        *(name.begin()+name.find(" ")+1)-=32;
        sname=name.substr(name.find(" ")+1);
        cout<<*(name.begin())<<"."<<" "<<*(name.begin()+name.find("1")+1)<<"."<<" "<<sname;
    }
    printf("\n");
}
return 0;
}
  • Recommendation. Replace all of those numbers with the appropriate characters. It better conveys the intent of your code. – user4581301 Jul 08 '17 at 21:15
  • Recommended reading: [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – user4581301 Jul 08 '17 at 21:16
  • First of all, you should at least use `cout << endl` instead of `printf("\n")` which is not c++. – Marco Luzzara Jul 08 '17 at 21:18
  • 1
    @MarcoLuzzara Agree, but watch out for `endl`. It's a newline and a stream flush, and that flush can get very expensive. – user4581301 Jul 08 '17 at 21:20
  • What exactly you are trying to do ?. Could you please helps us with input/output data. – Shravan40 Jul 08 '17 at 21:20
  • @Ron I am sorry but could you be more specific and point out the exact issue, I will appreciate it. – Vaibhav Gupta Jul 08 '17 at 21:22
  • @Shravan40 The problem is https://www.codechef.com/JULY17/problems/NITIKA , It is a simple problem. I just don't know what to do with the extra line I am getting – Vaibhav Gupta Jul 08 '17 at 21:25
  • As a new user you should take the tour and read the help page to get yourself familiar with the SO rules. Check out the [Asking](https://stackoverflow.com/help/asking) section on dos and donts when asking a question. That will help with your future inquiries. – Ron Jul 08 '17 at 21:25
  • 1
    Extra line of what? – user4581301 Jul 08 '17 at 21:25
  • @user4581301 If I run the code, after I input the t, the code outputs a random line, which it isn't supposed to do. – Vaibhav Gupta Jul 08 '17 at 21:27
  • 1
    Recommend picking something from the beginner section of [The Definitive C++ Book Guide and List](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Whatever resources you are using to learn to program are hopelessly inept. – user4581301 Jul 08 '17 at 21:27
  • 1
    Thanks for having the patience to tolerate my stupid question, just trying my hands on programming. I really appreciate the advises – Vaibhav Gupta Jul 08 '17 at 21:30
  • Based on the `bits/stdc++.h` included, you are using a g++-based compiler. These generally ship with a program called gdb. Your development environment may have a user interface built in to make gdb easier to use. If it does, I recommend using it. Regardless, run the program with gdb. You don't know where the extra line is coming from, so step through your program until it outputs the extra line, then inspect the state of the program, it's variables to determine what happened. – user4581301 Jul 08 '17 at 21:36
  • 1
    Aw smurf. I think I get it. After inputting the number do you press enter? – user4581301 Jul 08 '17 at 21:37
  • @user4581301 Yeah, I thought that is the problem too, but I knew if i said what I think the problem is, people will just bash me for not knowing how to fix that. – Vaibhav Gupta Jul 08 '17 at 21:40
  • 1
    Lovely how much downvoted is the question, even when community seems to be interested... – ipavlu Jul 08 '17 at 21:41
  • `cin>>t;` will read up to the newline that represents enter in the stream. `getline(cin,name);` will read up to the first newline in the stream and that's the first thing it will find so `getline` returns an empty string. You need to remove that newline before `getline` sees it. The safest thing to do is `cin.ignore(numeric_limits::max(), '\n')` to remove everything in the stream up to and including the first newline. – user4581301 Jul 08 '17 at 21:44
  • Here is a good write-up: https://stackoverflow.com/questions/25020129/cin-ignorenumeric-limitsstreamsizemax-n – user4581301 Jul 08 '17 at 21:47
  • @user4581301 yeah it worked !! Can't thank you enough. Thanks a lot !! – Vaibhav Gupta Jul 08 '17 at 21:47
  • ***Lovely how much downvoted is the question, even when community seems to be interested*** I did not downvote however I don't see this as a very useful question for future readers with the same problem so I can understand the downvote. – drescherjm Jul 08 '17 at 22:29

1 Answers1

0

Okay so, I don't really think there is any error, but I will propose you a modified version of your code, which will be more readable, more efficient, and less error prone:

// #include<bits/stdc++.h> I don't know what that include is, use more specific header based on your needs like
#include <string> // for std::string
#include <iostream> // for std::cout

// using namespace std;
// Avoid using "using namespace", it's a really bad practice in C++

int main()
{
  std::string name;
  std::string sname;
  int t;

  std::cin >> t;

  while(t--)
    {
      std::getline(std::cin, name);

      // *(name.begin()) -= 32;
      // Use operator[], it was made for that purpose
      name[0] -= 32;

      size_t pos = name.find(" ");

      if(pos == std::string::npos)
        {
          std::cout << name;
        }

      name[pos] = 49; // pretty dangerous since pos could be std::string::npos, which is the max value for a size_t

      pos = name.find(" ");

      size_t first_one = name.find("1");

      // here you should check if first_one != std::string::npos

      if(pos == std::string::npos)
        {
          name[first_one + 1] -= 32;
          sname = name.substr(name.find("1") + 1); // dangerous
          std::cout << name[0] << ". " << sname;
        }
        else
        {
          name[first_one + 1] -= 32;
          name[pos + 1] -= 32;
          sname = name.substr(name.find(" ")+1); // dangerous too
          std::cout << name[0] << ". " << name[name.find("1")+1] << ". " << sname;
        }
      // printf("\n"); Avoid using printf, std::cout is here for c++ (printf comes from C)
      std::cout << '\n';
    }
  return 0;
}

So to conclude, avoid "using namespace", that's bad, for explanation, google, first link, etc.. Use websites like cppreference.com or cplusplus.com to see methods and usages of objects like std::string or std::cout. Avoid doing std::cout << "a" << "b";, instead do std::cout << "ab";, or if you need to output single chars, std::cout << 'a';.

Verify everything, a big and very important rule for devs is "Never trust the user", always imagine they are gonna try to break your program, you might think "yeah but i will be the only user", that's a bad reason, take good habits early. So always check, here if t is negative, your program will be almost infinite, if there is no space or '1', it will explode, etc.. All of these small mistakes might be security flaws in bigger software.

Good luck :)

Ludonope
  • 963
  • 9
  • 14
  • I will keep all these points in mind next time, I have read no formal material on C++, just moved on from and C and improvised. Now I know how bad of a practice that is, thank you for all your time and patience. :) – Vaibhav Gupta Jul 09 '17 at 04:31
  • And someone already pointed out me the mistake, getline was just too excited to read /n. Thanks – Vaibhav Gupta Jul 09 '17 at 04:33