Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6109279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:23:13+00:00 2026-05-23T14:23:13+00:00

I had a problem with a function: int parsearRestricciones(char linea[], unsigned int& x, unsigned

  • 0

I had a problem with a function:

int parsearRestricciones(char linea[], unsigned int& x, unsigned int& y, unsigned int& tiempo, char restric[])

Inside that function I parse linea[].

The input consists in: three unsigned integers, and a string of punctuation characters. I need to read them that way.
The problem ocurrs when I assign atoi(linea+offset) to variable tiempo. Outside the function (i.e., in main() ), the value of tiempo is not the same that it’s inside.
I had the problem only with tiempo (I replaced x,y and tiempo by a pointer to struct. It works)

What could be the problem?

Thanks for your help.

—–edit-again

The full code:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <ctype.h>
#include <cassert>

#define MAX_RESTRIC 3  // Tres sentidos. Si hay 4, se usa '+'
#define MAX_LINEA 80
#define entrada cin


using namespace std;

int parsearRestricciones(char *linea, unsigned int& x, unsigned int& y, unsigned int& t, char *restric) {
// ! solo funciona si x,y,t,restric estan en una misma linea (i.e. no hay CR LF)

int i=0, j=0;

//Parsea x de la casilla
x = atol(linea+i);//strtol(linea,(char**)NULL,10);

while (isdigit(linea[i])) i++;
while (isspace(linea[i])) i++;

//Parsea y de la casilla
y = atol(linea+i);

while (isdigit(linea[i])) i++;
while (isspace(linea[i])) i++;
if(linea[i] == '\0')
    return -1;

//Parsea tiempo
t = atol(linea+i);
cout << "---" << t << endl;

while (!ispunct(linea[i])) i++;

//Parsea restricciones
while (linea[i] != '\0' && linea[i] != ' '){
    restric[j] = linea[i];
    i++; j++;
}
restric[j]='\0';

return 1;

}


int main (int argc, char** argv)
{
// Sugerencia de argumentos
// --d  (por Dijkstra)
// --axe  (por A*, distancia euclideana)
// --axm  (por A*, distancia manhattan)

// y dos màs que veremos luego :)

unsigned int X, Y;
unsigned int xi, yi;
unsigned int xf, yf;

unsigned int x,y,tiempo;

char restricciones[MAX_RESTRIC + 1];


//Buffer para parsear las lineas con restricciones
char linea[MAX_LINEA + 1];

bool finCasos = false;
bool siguienteCaso = false;


while (!finCasos)
{

    if (siguienteCaso){
        // Se leyó otro mapa antes que éste (hubo parseo, y quedo en xcasilla,ycasilla)
        X = x;
        Y = y;
        siguienteCaso = false;

    } else {
        // Sino, lee por primera vez las dimensiones del mapa
        entrada >> X >> Y;
    }

    if ( X == 0  &&  Y == 0 )
        finCasos = true;

    else {

        entrada >> xi >> yi;

        entrada >> xf >> yf;

        // Lee restricciones hasta que encuentra una linea sin ellas (sin tiempo ni direccion)
        // se asumira, que corresponde a las dimensiones del siguiente caso, y los usará en la siguiente
        // iteracion
        while(!siguienteCaso) {

            cin.get(); //lee un '\0' que quedó (?)
            cin.getline(linea, MAX_LINEA+1);

            if ( parsearRestricciones(linea,x,y,tiempo, restricciones) == -1 ) {
                siguienteCaso = true;

            } else {

                cout << "X = " << x << endl;
                cout << "Y = " << y << endl;
                cout << "tiempo = " << tiempo << endl;
                cout << "restric = " << restricciones << endl;
                int j=0;
                cout << "restric = " ;
                while(restricciones[j]!='\0'){
                    cout << restricciones[j];j++;}
                cout << endl;

                //-- agregar datos al grafo/mapa
            }

        }

        // Resolver usando algun algoritmo
        //--- resolver(MAPA)

    }

}


return 0;
}

CFLAGS. -Wall -pipe -g -ggdb -DONLINE_JUDGE -DNDEBUG
(The Makefile also builds another source, for uvaonlinejudge)

Input:

101
10
1
1
2
2
1000 10000  100000 +++++

Output:

---100000
X = 1000
Y = 10000
tiempo = 65579
restric = +++++
restric = +++++
^X^C (I did break)

I just tested the program in Windows (using Code::Blocks, default settings) and it worked :/

By the way, I’m using Ubuntu in Virtualbox

Could you tell me what I’m doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T14:23:13+00:00Added an answer on May 23, 2026 at 2:23 pm

    How many parameters can I pass by reference in C++, without getting abnormal behavior?

    As many as you want to!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Over the weekend, I had specific problem with function overload resolution that I can't
I had a problem that was partially solved. To explain it quickly : I
I had a frustrating problem recently that boiled down to a very simple coding
I have some code that I had to write to replace a function that
I originally had a function that uses pow(), and I noticed it was returning
I made a simple function, which get's a file's size. int file_size( char *
I had to write a merge sort function in Java. No problem. Well, a
I had a problem with committing changes after merging two branches of my project
I had this problem before and can't for life of me remember how to
I had no problem at all running the following code on a local server,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.