having problems with atoi and displaying output

Hello everyone,

I'm in a pinch right now as I should send this as an homework tomorrow.I couldn't figure out what the problem is for a very very long time and I'm new to these things.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include "strutils.h"
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

bool inputcheck(string s)
{
	int i;
	if(s.at(0) == '-')
	{
		i = 1;
	}
	else if (s.at(0) == '+')
	{
		i = 1;
	}
	else
	{
		i = 0;
	}	
	while(i < s.length())
	{
	if( (s[i]<'0') ||(s[i]>'9') )
	{
		return false;
		i++;
	}
		
	if((i == 1) && ((s.at(0)=='-') || (s.at(0)== '+')))
	{
		return false;
	}
	else
	{
		return true;
	}
	}
}
int int_length(string s)
{
	int i;
	if(s.at(0) == '-')
	{
		i = 1;
	}
	else if (s.at(0) == '+')
	{
		i = 1;
	}
	else
	{
		i = 0;
	}
	while(i < s.length())
	{
		if( (s[i]<'0') ||(s[i]>'9') )
			return i;

		i++;
	}
	if((i == 1) && ((s.at(0)=='-') || (s.at(0)>'+')))
	{
		return 0;
	}
	else
	{
		return i;
	}
}
	
int main()
{
	int x,y;
	string input,input1;
	cout<<"Please enter the initial coordinate of the agent: "<<endl;
	cin >> input;
	if((input.at(0) == '(') && (input.at(input.length()-1) == ')'))
	{
		int pos= input.find(',');
		int pos_reverse= input.rfind(',');
		if (( pos != string::npos) && (pos_reverse == pos))
		{
			string xstr,ystr;
			xstr = input.substr(1, pos-1);
		    ystr = input.substr(pos+1, input.length()-pos-2);
			if( inputcheck(xstr) && inputcheck(ystr))
			{
				
			stringstream(xstr)>>x;
			
			stringstream(ystr)>>y;
			}
		}
			
	}
	cout<<"Please enter the action to execute: "<<endl;
	cin >> input1;
	while(input1.length() > 0)
{
	if(input1.find("west") == 0) 
	{
		input1 = input1.substr(4);
		int a=int_length (input1);
		if(a == 0)
		{
			
			input1=input1.substr(a);        //This parts are the problem I think.
			x+=atoi(a);                              
		}
	}
	else if(input1.find("south") == 0)
	{
		input1 = input1.substr(5);
		int a=int_length (input1);
		if(a == 0)
		{
			input1 = input1.substr(a);    //and this part
			y-=atoi(a);
		}
	}
	else if(input1.find("north") == 0)
	{
		input1 = input1.substr(5);
		int a=int_length (input1);
		if(a == 0)
		{
			
			y+=atoi(a);                                  //and this part
			input1=input1.substr(a);
		}
	}
	else if(input1.find("east") == 0)
	{
		input1 = input1.substr(4);
		int a=int_length (input1);
		if(a == 0)
		{
			int a=int_length (input1);                 //and this part
			x+=atoi(a);
			input1=input1.substr(a);
		}
	}
	else
	{
		cout<<"Error"<<endl;
	}
	system("pause");
}
}

The program output should be something like this;

Please enter the initial coordinate of the agent: (3,5)
Please enter the action to execute: north3
The agent moves to: (3,8)

I couldn't figure out how to print out the "(3,8)" part and the program gives error with those atoi's above.What can I use instead of atoi and how can I print out the "(3,8)" part? Or are there any other problems besides these? Any help is highly appreciated.
Last edited on
you mean this?
cout << "The agent moves to: ("<< x << "," << y << ")" << endl;

and i think asking for x and y seperately would be alot easier instead of getting coordinates and messing with those
(looks cooler though ;D)
Last edited on
In the line input1=input1.substr(4), you are basically deleting anything that isn't in that substring. So "west5" becomes "west" and you lose "5".

Plus, I'm not even sure what the logic behind the other functions is meant to be.
If you want to convert a string to integer, rather than use atoi() you could use std::istringstream():
1
2
3
4
5
6
7
8
9
int i;
if(std::istringstream(str) >> i)
{
    // str was a valid integer so i is good
}
else
{
    // str was not a valid integer ... i is bad
}
Topic archived. No new replies allowed.