Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
Photo

beginner C++ Question

C++

  • Please log in to reply

#1
Cttyssha

Cttyssha

    New Member

  • Member
  • Pip
  • 1 posts

I figured instead of registering on another forum, I'd come here and maybe someone can answer my basic question. Right now I am testing my development skills and trying a simple exercise. I am trying to produce a program that will take a user inputed string and convert each individual character to the numeric value of the alphabet (e.g. A=/1/, Z=/26/) 

This is nothing more then a basic string crypter and soon I will program a decrypter. I have got a little bit of it through previous knowledge and looking up some stuff but now I am stuck as to what to do. Here is what I have so far:
 

Code:
#include <iostream>
#include <string>
using namespace std;

int main () {
    string uncrypt;
    int num;
    int c;
    cout << "Please enter your word to be crypted: ";
    cin >> uncrypt;
    num = uncrypt.length();
    for(c = 0; (c = num); c++)
    {
        //Stuff Here
    }
    
    
    }

 


  • 0

Advertisements


#2
Spike

Spike

    nOoB

  • Member
  • PipPipPipPip
  • 1,355 posts

I figured instead of registering on another forum, I'd come here and maybe someone can answer my basic question. Right now I am testing my development skills and trying a simple exercise. I am trying to produce a program that will take a user inputed string and convert each individual character to the numeric value of the alphabet (e.g. A=/1/, Z=/26/) 

This is nothing more then a basic string crypter and soon I will program a decrypter. I have got a little bit of it through previous knowledge and looking up some stuff but now I am stuck as to what to do. Here is what I have so far:
 

Code:
#include <iostream>
#include <string>
using namespace std;

int main () {
    string uncrypt;
    int num;
    int c;
    cout << "Please enter your word to be crypted: ";
    cin >> uncrypt;
    num = uncrypt.length();
    for(c = 0; (c = num); c++)
    {
        //Stuff Here
    }
    
    
    }

 

 

Hey Cttyssha and welcome to Geeks to Go,

 

I have not compiled your code or anything but have spotted an issue in your loop control statement...

 

A for loop is in the format:

for( [loop variable initialization and start value], [execution condition], [increment variable] )

 

The problem existing in your [execution condition] where by the value requires being true in order to execute the code in the loop statement. When this value gets set to false at some point the loop will stop and not execute longer and your program will continue as normal executing anything after the loop.

 

The first issue is that you are not using a comparison operator == and instead using the assignment operator =. I stand corrected, but your compiler should complain about this statement as the second parameter expected a boolean type value...

 

The second issue is that even if you did use a comparison operator ==, your condition reads: (c == num) where on the first loop where c = 0, num will hold the length on the string captured by the input stream; this means unless the string length is equal to 0 your loop [execution condition] will result in a false value.

 

Usually a template for "for loops" [execution condition] in the form you have it is as follows:

 

for(c = 0; c < num; c++)

 

So to elaborate on the condition where c < num, will always execute as true as long as "num" is less than "c"; therefor your loop should execute "num" times until the condition becomes false as "c" gets incremented.

 

I hope this somewhat assists you... Please let me know if you need any other explanation or assistance.

 

Peace Out :cool:

Spike


  • 0

#3
Kerysi

Kerysi

    New Member

  • Member
  • Pip
  • 1 posts

There are a few easy ways to do this, you can take the letter 'A' and do this with the character data type I believe. But I havent done it in some time and I dont feel like looking up how to do it (I think you just convert it to an integer type), tbh im not even really sure if its c++ that does it or another language...

If you're just starting out, I dont really want to throw type conversions at you and such, so theres the easy intuitive way that works.

Make an array that contains a-z, use the position + 1 to figure out which number it is in the array/alphabet

so if you look up A, youll return 0, add 1, youll get 1. B, 1, +1=2 etc

the method I gave you is only really good if you just want to tinker with c++ as a beginner and just want to get it to work. Im almost certain most languages contain features to do exactly what you are talking about, but im terrible at remembering specific things, and i often rely on documentation if i havent used something in a while

https://diceus.com/1...evelopers-2018/


Edited by Kerysi, 22 April 2019 - 10:32 PM.

  • 0






Similar Topics


Also tagged with one or more of these keywords: C++

1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP