0

I'm currently learning c++ from scratch, I've previously developed apps with C# using Visual Studio but I'm a total noob with C++, I'm trying to make a small console exe that releases and renews the ip while practicing using headers and differents .cpp files.

The issue is that when I run the local windows debugger from visual studio 2015 the code runs perfectly and does everything I'm trying to. But when I build and try to run the .exe file it goes into an infinite loop stating endlessly the output from the std::cout <<"Realizando ipconfig Release", I have localized the issue to when it tries to run the system("ipconfig /release"). Why does this happen? and How can I fix it?

This is the header

#pragma once
#ifndef HeaderIp
#define HeaderIp
int Release();
int Renew();
#endif // !HeaderIp

This is the release.cpp

   #include <stdlib.h>
int Release()
{
    if (system(nullptr)==0)
    {
        return 0;
    }
    else
    {
        system("ipconfig /release");
        return 1;
    }
}

This is the renew.cpp

#include <stdlib.h>;
int Renew()
{
    if (system(nullptr)==0)
    {
        return 0;
    }
    else
    {
        system("ipconfig /renew");
        return 1;
    }
}

and finally this is the ipconfig.cpp

#include <iostream>
#include <stdlib.h>
#include "HeaderIp.h"
#include <stdio.h>

int main()
{   
    std::cout << "Realizando ipconfig Release" << std::endl;
    int i = 0;                                                      //Bit de informacion de status de CPU
    i = Release();
    if (i == 0)
    {
        std::cout << "Error al liberar IP" << std::endl;
        system("pause");
        exit;
    }
    else
    {
        std::cout << "Ip Liberado correctamente" << std::endl;
    }
    i = Renew();
    if (i == 0)
    {
        std::cout << "Error al renovar IP" << std::endl;
        system("pause");
        exit;
    }
    else
    {
        std::cout << "Ip renovado correctamente" << std::endl;
    }

system("pause");
return 0;

}
Joesteva
  • 1
  • 2

1 Answers1

0

It's unusual, and indeed generally discouraged to use system in C++. If you were to manage DHCP leases using IpReleaseAddress and IpRenewAddress, instead, you might find your problem disappears.

You can use std::cin.sync() instead of system("pause"), too.

exit; probably doesn't do what you want it to, as you're only referring to the function, you're not calling it, so... it'll do nothing.

The semicolon in #include <stdlib.h>; is an error, and you should be including <cstdlib> and <cstdio> rather than <stdlib.h> and <stdio.h>.

Community
  • 1
  • 1
autistic
  • 1
  • 3
  • 35
  • 80
  • Actually the answer to my issue was what @Tas stated, it was recursively calling itself as I stated above in a comment, I changed the name of the program and it does exactly what I was trying to, but thanks for the heads up on the IpReleaseAddress and IpRenewAddress I will check on them. – Joesteva Apr 19 '17 at 12:56
  • @Joesteva Precisely. If you removed the `system` call and instead used the API functions I mentioned (like a **normal** programmer would), your program would not recursively invoke itself, because there's no `system` call to cause that. – autistic Apr 19 '17 at 18:44