#include <iostream>
using namespace std;
int main()
{
unsigned int loopindex[1024];
unsigned int loopdepth=0;
char code[4096];
char data[4096];
unsigned int codep=0;
unsigned int datap=0;
cin >> code;
while(code[codep]!=0)
{
if(code[codep]=='+') data[datap]++;
else if(code[codep]=='-') data[datap]--;
else if(code[codep]=='>') datap++;
else if(code[codep]=='<') datap--;
else if(code[codep]=='.') cout << data[datap];
else if(code[codep]==',') cin >> data[datap];
else if(code[codep]=='[')
{
loopdepth++;
loopindex[loopdepth]=codep;
}
else if(code[codep]==']')
{
if(data[datap]) codep=loopindex[loopdepth];
else loopdepth--;
}
codep++;
}
cin.ignore();
cin.get();
}
I have a question about it, though: How could I cast the input to an int?
8 replies to this topic
#1
Posted 04 November 2010 - 05:12 PM
I made a brainfuck interpreter, and I thought I would share it:
Latinamne loqueris?
|
|
|
#2
Posted 06 November 2010 - 09:18 AM
Hi
You can have a look how to do castingin C++
Munir
You can have a look how to do castingin C++
Munir
#3
Posted 06 November 2010 - 12:11 PM
I know, I tried that. I also tried reinterpret_cast<>() but that didn't work either.
Latinamne loqueris?
#4
Posted 06 November 2010 - 02:00 PM
From string to int? Have you tried stringstream?
A conclusion is where you got tired of thinking.
#define class struct // All is public.
#5
Posted 06 November 2010 - 02:20 PM
Hi mebob,
You need strtol function. It parses the C string interpreting its content as an integral number and allows you to parse numbers in various formats (i.e. 100, 0xff). Example:
You need strtol function. It parses the C string interpreting its content as an integral number and allows you to parse numbers in various formats (i.e. 100, 0xff). Example:
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
char str[] = "2010 7"; // your string (array of chars)
char * pEnd; // pointer to the current position in array we are parsing
long year = strtol(str, &pEnd, 10);
cout<<"Parsed "<<(pEnd - str)<<" chars, year: "<<year<<endl;
long month = strtol(pEnd, &pEnd, 10); // 10 is a base of a number
cout<<"Parsed "<<(pEnd - str)<<" chars, month: "<<month<<endl;
}
#6
Posted 06 November 2010 - 06:32 PM
It's not getting a string as input, it's only getting a single character.
Latinamne loqueris?
#7
Posted 07 November 2010 - 01:45 AM
Then you can try atoi function:
char your_char = '5'; int your_int = atoi(&your_char); # your_int = 5or simply assign char to int:
char your_char = 'a'; int your_int = your_char; # your_int = 97, 97 is the ASCII number of char 'a'
#8
Posted 07 November 2010 - 07:54 AM
Neither of them will work: I know how to use the atoi() function, but it only works with null terminated strings (as far as I know), and I only have a single character, which limits me to the numbers 0-9. The second won't work either, because I want the input to be an int, not a character.
Latinamne loqueris?
#9
Posted 07 November 2010 - 12:32 PM
char* foo = "43210"; //non-null terminated multicharacter string
int bar = atoi(foo);
printf("%i", (int)(bar == 43210));
And if you wanted only 0-9, you can do this: char foo = '4';
int bar = (int)foo - '0';
printf("%i", (int)(bar == 4));
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









