-
Basic to C++ Translation
I am currently having problems making my basic prog a cpp prog.
Here's what I am trying to do.
Step 1) Read a string from a file.
Step 2) Read the string character by character.
Step 3) Get each character's ascii value into an integer.
Step 4) Get the character back into the string.
Step 5) Put the character back into another string, in the same order.
Steps 2 - 5, I don't know in C++, reading files, I know.
Here is my VB Code:
open "FILE" for input as 1
input #1, string$
close 1
a = len(string$)
for i = 1 to a ' For loop that goes until the end of the string
b$ = mid(string$, i, 1) ' b$ become 1 character each cycle.
c = asc(b$) ' c equals the ascii value of b$
c = c + 5 ' self explanatory
b$ = chr$(c) ' b$ = the new character
d$ = d$ + b$ ' d$ become the new string.
next i
print d$
say the string was: HI
the output would be: MN
I want to do this in C++, but have no clue where to start.
Any help would be great.
-
First place to start is by opening a file. You need the <iostream> header file. Look at your documentation/books/internet for information about the classes contained in this library and examples on how to use them. The class you want to use is std::ifstream (standard library : input file stream). After you're done that you can use a function in that class (not telling which) where you can get a line of chars and store it in a buffer where you can use as an array to get each character. Look at the header file <cstring> for functions dealing with strings. You especially want one that returns the length of a string (so you can loop each character).
Optionally, you can use a std::string class contained in <string>. You will have to read up a lot upon that and although its easier than using c-strings (char buffers), it requires you to learn about iterators.
In order for you to learn the language, you need to be able to use your help files. You should be doing that a lot.
-
Jesus christ, could you make the process sound any more impossible. I mean Speedy5 is right that you should do all of that reading but you could probably get enough information out of one or two tutorials to make your code work. Just don't think anyone here is going to do work for you.
-
i not too long ago switched from vb to c++ and trust me the change can be quite anoying at times (however once u get used to it you will see how sloppy vb code can look in comparrison) ive coded your app for u from vb to c++ trying to use the same syntax 'vb style' coding and variable names, its quite straight (cant spell) foward:
Code:
#include "fstream.h"
#include "string.h"
#include "windows.h"
int OpenFile(char FileName[255]);
char filedat[255];
int main(int argc, char* argv[])
{
OpenFile("d:\\file.txt");
int a = strlen(filedat);
int i = 0;
while(i<=a)
{
filedat[i]=(char)(((int)filedat[i]) + 5);
++i;
}
MessageBox(NULL,filedat,"Outcome",0);
return 0;
}
int OpenFile(char FileName[255])
{
fstream File;
File.open(FileName,strlen(FileName),ios::in);
File.get(filedat,255,'\n');
File.close();
return 1;
}
hope it helps
-
Or a non platform-specific version:
Code:
#include <iostream>
#include <fstream>
int main( void ) {
std::ifstream in( "FILE" );
char ch;
while( in.get( ch ) )
std::cout<<(char)(ch + 5);
}
-
Well XSquared, he only specified to read in a string from the file, not read the entire file. And that means I'm also wrong (partly). I told him to get a line from the file except that Input # with one variable passed to it only reads one number, one date, one string. The problem is that Input # reads one word UNLESS if its in between quotes which it them reads however many words are in between. So then in C++ he must choose: read a whole line, read a single word (filestream >> word), or make a parser for himself that will work just like VB.
And MicroFiend: there is no reason why OpenFile() returns an int. It should return void.
-
You know, you don't have to rub it in my face that I don't know EVERYTHING there is to know, Master....
Everyone else, thank you for trying to help me.
It is greatly appreciated.
-
lol
i see what u mean about returning void but ahwel thats just my habbit of programming i prefer to give return values for my subs so i know when an error occours (i hate spelling) i know where to find it besides im an assembly programmer aswel so if i ever wanna decompile my apps i know where to look
-
Thank you very much
Microfiend,,
filedat[i]=(char)(((int)filedat[i]) + 5);
This is exactly what I needed to know, it works great, thank you very much, I didn't know you could change each individual char within an existing char array. But now I know, and I appreciate it emensly
TTFN
-
No Probs
no problem,
you can do the same in vb i do it that way as it saves time and looks neater ^^
also i find it nice using a type cast '(int)' rather than having to use a function 'asc()' (one of the reasons i moved to using c)