Thread: Basic to C++ Translation

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    24

    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.
    Keep smiling, it makes the big guys wonder what you're up to.

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    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.

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    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.

  4. #4
    Registered User MicroFiend's Avatar
    Join Date
    Nov 2002
    Posts
    80
    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

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    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);
    
    }
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    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.

  7. #7
    Registered User
    Join Date
    Jun 2002
    Posts
    24
    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.
    Keep smiling, it makes the big guys wonder what you're up to.

  8. #8
    Registered User MicroFiend's Avatar
    Join Date
    Nov 2002
    Posts
    80

    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

  9. #9
    Registered User
    Join Date
    Jun 2002
    Posts
    24

    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
    Keep smiling, it makes the big guys wonder what you're up to.

  10. #10
    Registered User MicroFiend's Avatar
    Join Date
    Nov 2002
    Posts
    80

    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)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic to C translation - Need Help
    By Claudio.r in forum C Programming
    Replies: 1
    Last Post: 06-28-2009, 03:56 PM
  2. [ANN] New script engine (Basic sintax)
    By MKTMK in forum C++ Programming
    Replies: 1
    Last Post: 11-01-2005, 10:28 AM
  3. what are your thoughts on visual basic?
    By orion- in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 09-22-2005, 04:28 AM
  4. visual basic vs C or C++
    By FOOTOO in forum Windows Programming
    Replies: 5
    Last Post: 02-06-2005, 08:41 PM