-2

While I've seen some people answer this for Python, I don't know how to do so in C++. Bear in mind, I'm still learning.

As the title should tell you, I'm trying to define multiple variables in a C++ program via a while loop. Specifically, I am trying to make it so it will read through the first 8 characters of a string, and then assign them to their dedicated char values of c1, c2, c3, c4, c5, c6, c7, and c8. However, I can't figure out an easy way to make it do this.

For an example, it should see the string "FAR OUT!" and print the following characters to each c1-8 variable:

  1. F
  2. A
  3. R
  4. (space)
  5. O
  6. U
  7. T
  8. !

The most recent attempt seems to believe I am referring to a variable called "c", and not consider the fact I'm telling it to take the value of the variable check, add 1 to it, and then add that number to the end of c, and then take the variable that is that output.

#include <iostream>
#include <string>

using namespace std;

int main()
{
    // varaible declaration(s)
    char c1;
    char c2;
    char c3;
    char c4;
    char c5;
    char c6;
    char c7;
    char c8;
    string phonenumber;
    int check = 0;

    // accept user input
    cout << "Phone number in letters: ";
    cin >> phonenumber;

    // convert string to char
    while(check < 8)
    {
        phonenumber[check] = c(check + 1);
        check = check + 1;
    }
    // start while loop

    // program logic and output
    cout << endl << c1 << c2 << c3 << c4 << c5 << c6 << c7 << c8 << endl;
}
SirDarius
  • 41,440
  • 8
  • 86
  • 100
  • 4
    `while (condition) { /* loop body */ }` Without the `{` and `}`, only the *next* statement is included in the loop. – crashmstr Oct 03 '17 at 15:59
  • 3
    Why not use a `std::array chars;` if what you want is 8 `char`s? – Jesper Juhl Oct 03 '17 at 16:01
  • 3
    Why are you doing this? You can either copy the first 8 characters from the string into another one or just use the string and reference the character directly like `string_name[index]`. – NathanOliver Oct 03 '17 at 16:01
  • Um, no. You can't create variable names at runtime like this: `c(check + 1)`. – Carey Gregory Oct 03 '17 at 16:02
  • It looks like you are trying to take the strings 1, 2, etc.. and concatenate them with the variable `c` so that you can reference the variables `c1`, `c2`, etc... Not only will that not work but even if it did you wouldn't want to do it. You should use an array instead. – takendarkk Oct 03 '17 at 16:02
  • I literally am still new to this, guys; I haven't even learned in-depth as to what an array is, for goodness sake's. That's how new I am to this.Also, bear in mind I have the entire standard and string libraries added. Mostly since that's how I was taught to do so in cases where there'd be no contradicting names. – Camwoodstock Oct 03 '17 at 16:03
  • (Also, note I'm not trying to concentrate anything on variable c. In fact, variable c isn't supposed to exist; it's the odd way the program is interpreting it... also, c1-c8 are all pre-defined as is, so it's not like i'm pulling odd new variables onto the program... at least I think) – Camwoodstock Oct 03 '17 at 16:03
  • Nothing odd about about it. It's not valid C++. – Carey Gregory Oct 03 '17 at 16:04
  • I guess that's a fair point, though that raises the question as to what IS valid C++ to tell it to do that. – Camwoodstock Oct 03 '17 at 16:05
  • See the answer by @freakish. That's how you tell it to do that. – Carey Gregory Oct 03 '17 at 16:05
  • Please explain what you thought this piece of code was doing or what you expected it to do. `c(check + 1)`. – takendarkk Oct 03 '17 at 16:06
  • Simply put, it would read the first 8 characters of string phonenumber, and then imprint the first character (or character 0, since arrays start at 0 on _good_ programming languages) into c1, the second character into c2, so on so forth until c8. If it detects it's trying to read a ninth character, it stops. – Camwoodstock Oct 03 '17 at 16:11
  • 1
    C++ is a compiled language so you can't form variable names at runtime. That means this can't be done with a loop if you won't use an array. You're just going to have to assign each variable separately; eg, `phonenumber[0] = c1; phonenumber[1] = c2;` and so on. However, you haven't assigned anything to the "c" variables so this does nothing useful. – Carey Gregory Oct 03 '17 at 16:16

2 Answers2

3

Why don't you just use a char array?

#include <array>
...

// instead of c1, c2, ...
std::array<char, 8> c;
// later
while (condition) {
    // read or set here, whatever you need
    c[check] = some_value;
    ...
}
freakish
  • 54,167
  • 9
  • 132
  • 169
  • Thing is, I don't really know HOW to use arrays. Just saying I can use one doesn't really help out much when I don't know how one works in the first place... and as I said before, the entire standard library is in there, so the whole `std::` prefix is kinda moot. – Camwoodstock Oct 03 '17 at 16:09
  • 1
    @Camwoodstock dig into any tutorial. Google it. The topic is waaay to broad for SO. That's all I can do for you. – freakish Oct 03 '17 at 16:11
  • 2
    @Camwoodstock Or, alternatively, pick up a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Algirdas Preidžius Oct 03 '17 at 16:12
  • Okay, I've tried adding the array; problem is, I don't know how to set the variables to their proper characters. – Camwoodstock Oct 03 '17 at 16:14
  • 1
    @Camwoodstock You need a book or a class.This isn't how you learn a language. – Carey Gregory Oct 03 '17 at 16:17
  • I'm IN one. This is actually one of the assignments I'm meant to do... I get the feeling they probably didn't scale it too well if this is assigned before I even learn what an array is. – Camwoodstock Oct 03 '17 at 16:20
0

A while loop won't work for this. The names of variables are determined when the code is compiled; you can't make variable names on the fly when the program is running.

So don't use the loop. After you've read phonenumber, copy the values into the appropriate variables:

c1 = phonenumber[0];
c2 = phonenumber[1];

and so on. Of course, if phonenumber has fewer than 8 characters in it, you've got a problem...

Pete Becker
  • 74,985
  • 8
  • 76
  • 165
  • Ah, so my method's pretty much moot. Thanks, man. It might not really be the most helpful answer to people in the future, but this made the most sense. – Camwoodstock Oct 04 '17 at 12:51