#Convert int -- to a string

44 messages · Page 1 of 1 (latest)

hasty bramble
#
//  main.cpp
//  Horse Race A3
//
//  Created by Davon Raymond on 8/23/22.
//
/*-----Libraries-----*/

#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <string>
using namespace std;
#include <sstream>

/*-----Variables-----*/
int randomNumber = 0;
const int row = 2;



/*------Shifting Function to allow all variables to move right-----*/

void shift(int values[], int size) {
      int temp = values[size-1], temp1;
      for (int i = 0; i < size; i++) {
           temp1 = values[i];
          values[i] = temp;
          temp = temp1;
          cout << values[i];
      }
   cout << endl;
 }

    
/*-----Main Funciton */

int main()
{
    /*-----Variables-----*/

    
    //Main Function Code

    
   cout<<"Before: "<< endl;
   const int col = 5;
    int num[5];

    for(int i = 0; i < col; i++){
        string num_to_symbol = to_string(num[i]);

        num[i] = num_to_symbol;
        cout<< num[i];
        
       
        }
    cout<<endl;
        cout << "shifting all elements to the right: " << endl;
    shift(num, col);
    cout << endl;
    shift(num, col);
    cout << endl;
    shift(num, col);
    cout << endl;
        system("pause");
        return 0;
    } 
#

!run

#

Convert int -- to a string

hasty bramble
rich flareBOT
#

@hasty bramble's code (missing deletion permissions), requested by @minor dagger:

//
//  main.cpp
//  Horse Race A3
//
//  Created by Davon Raymond on 8/23/22.
//
/*-----Libraries-----*/

#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;
#include <sstream>

/*-----Variables-----*/
int randomNumber = 0;
const int row = 2;

/*------Shifting Function to allow all variables to move right-----*/

void shift(int values[], int size) {
    int temp = values[size - 1], temp1;
    for (int i = 0; i < size; i++) {
        temp1 = values[i];
        values[i] = temp;
        temp = temp1;
        cout << values[i];
    }
    cout << endl;
}

/*-----Main Funciton */

int main() {
    /*-----Variables-----*/

    // Main Function Code

    cout << "Before: " << endl;
    const int col = 5;
    int num[5];

    for (int i = 0; i < col; i++) {
        string num_to_symbol = to_string(num[i]);

        num[i] = num_to_symbol;
        cout << num[i];
    }
    cout << endl;
    cout << "shifting all elements to the right: " << endl;
    shift(num, col);
    cout << endl;
    shift(num, col);
    cout << endl;
    shift(num, col);
    cout << endl;
    system("pause");
    return 0;
}
minor dagger
#

@hasty bramble Why are you converting num[i] to a string then trying to assign it back to an int? That makes no sense 😛

hasty bramble
#

basically i want the [1......} to go to the end {....1}

#

i was just trying to understand the shift algorithm before i complicated the code

#

but the movement would depend on a flip coin randomizer

#

rand %2

#

if chance==1

#

shift right

#

@minor dagger

minor dagger
#

Why are you trying to convert to a string?

hasty bramble
#

i think i see my mistake on here that your sayign so initially i had the flip coin funciton but i took it out

#

so values[size] = {".",".",".","."}

#

if flip coin ==1

#

num[i] = 1;

#

else

#

cout num[i]

#

!f

rich flareBOT
#

Formatting Error
@hasty bramble, nothing to format! Please reply to the message you want to have formatted.

hasty bramble
#

!f

rich flareBOT
#

@hasty bramble, your formatted code (missing deletion permissions):

//
//  main.cpp
//  Horse Race A3
//
//  Created by Davon Raymond on 8/23/22.
//
/*-----Libraries-----*/

#include <cstdlib>
#include <iomanip>
#include <iostream>
using namespace std;

/*-----Variables-----*/
int randomNumber = 0;
const int row = 6;

/*------Shifting Function to allow all variables to move right-----*/

void shift(int values[], int size) {
    int temp = values[size - 1], temp1;
    for (int i = 0; i < size; i++) {
        temp1 = values[i];
        values[i] = temp;
        temp = temp1;
        cout << values[i];
    }
    cout << endl;
}

/*-----Coin Toss Funciton -----*/
/*
void coinToss()
{
    int randomNumber;
    randomNumber = 1 + rand() % 2;


}
*/

/*-----Main Funciton */

int main() {
    /*-----Variables-----*/
    string values[] = {".", ".", ".", ".", ".", ".", ".", ".",
                       ".", ".", ".", ".", ".", "."};  // 14 plus the begingin
                                                       // number in the stable

    // Main Function Code
    cout << "5 Horses in the stable " << endl;

    const int CAP = 5;
    int numbers[CAP];

    int i;
    for (i = 0; i < CAP; i++) {
        int rng = rand() % 2;

        while (rng == 1) {
            // this is where the shift happens
            numbers[i] = rng;
            cout << numbers[i];
        }
    }
    cout << "shifting all elements to the right: " << endl;
    shift(numbers, CAP);

    cout << endl;
    system("pause");
    return 0;
}
hasty bramble
#
//  main.cpp
//  Horse Race A3
//
//  Created by Davon Raymond on 8/23/22.
//
/*-----Libraries-----*/

#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;


/*-----Variables-----*/
int randomNumber = 0;
const int row = 6;



/*------Shifting Function to allow all variables to move right-----*/

void shift(int values[], int size) {
      int temp = values[size-1], temp1;
      for (int i = 0; i < size; i++) {
           temp1 = values[i];
          values[i] = temp;
          temp = temp1;
          cout << values[i];
      }
   cout << endl;
 }

/*-----Coin Toss Funciton -----*/
/*
void coinToss()
{
    int randomNumber;
    randomNumber = 1 + rand() % 2;

   
}
*/
    
/*-----Main Funciton */

int main()
{
    /*-----Variables-----*/
    string values[] = { ".", ".", ".",".","."}; //5 plus the begingin number in the stable

    
    //Main Function Code
    cout << "5 Horses in the stable " << endl;
    
    
    
 
        const int CAP = 5;
        int numbers[CAP];

        int i;
        for (i = 0; i < CAP; i++) {
            int rng = rand() % 2;
            while(rng ==1)
            {
            //this is where the shift happens
            numbers[i] = rng;
            cout << numbers[i];
            }
        
            
            
        }
        cout << "shifting all elements to the right: " << endl;
    shift(numbers, CAP);

        cout << endl;
        system("pause");
        return 0;
    }
    
    
    
    
    
    
    
    /*-----Funciton for shifting ------*/
   /* cout << "Random 10 index array" << endl;
        const int CAP = 10;
        string values[CAP];
        int i;
        for (i = 0; i < CAP; i++) {
            cout << values[i];

        }
        cout << "shifting all elements to the right: " << endl;
    shift(values, CAP);

        cout << endl;
        system("pause");
    
    */
    
    
    
    /*-----Coin Toss-----*/





rich flareBOT
#

@hasty bramble, your formatted code (missing deletion permissions):

//
//  main.cpp
//  Horse Race A3
//
//  Created by Davon Raymond on 8/23/22.
//
/*-----Libraries-----*/

#include <cstdlib>
#include <iomanip>
#include <iostream>
using namespace std;

/*-----Variables-----*/
int randomNumber = 0;
const int row = 6;

/*------Shifting Function to allow all variables to move right-----*/

void shift(int values[], int size) {
    int temp = values[size - 1], temp1;
    for (int i = 0; i < size; i++) {
        temp1 = values[i];
        values[i] = temp;
        temp = temp1;
        cout << values[i];
    }
    cout << endl;
}

/*-----Coin Toss Funciton -----*/
/*
void coinToss()
{
    int randomNumber;
    randomNumber = 1 + rand() % 2;


}
*/

/*-----Main Funciton */

int main() {
    /*-----Variables-----*/
    string values[] = {".", ".", ".", ".",
                       "."};  // 5 plus the begingin number in the stable

    // Main Function Code
    cout << "5 Horses in the stable " << endl;

    const int CAP = 5;
    int numbers[CAP];

    int i;
    for (i = 0; i < CAP; i++) {
        int rng = rand() % 2;
        while (rng == 1) {
            // this is where the shift happens
            numbers[i] = rng;
            cout << numbers[i];
        }
    }
    cout << "shifting all elements to the right: " << endl;
    shift(numbers, CAP);

    cout << endl;
    system("pause");
    return 0;
}

/*-----Funciton for shifting ------*/
/* cout << "Random 10 index array" << endl;
     const int CAP = 10;
     string values[CAP];
     int i;
     for (i = 0; i < CAP; i++) {
         cout << values[i];

     }
     cout << "shifting all elements to the right: " << endl;
 shift(values, CAP);

     cout << endl;
     system("pause");

 */

/*-----Coin Toss-----*/
vale void
#

sprintf(string, "%d", number);

shell stirrup
#

;compile ```c++
#include<iostream>
int main() {
std::cout << 123;
}

tired scarabBOT
#
Program Output
123
hasty bramble
#

{1......}

#

coin flip

#

then if head rotate right

shell stirrup
# hasty bramble I want to print "." on the empty spaces

;compile c++ ```c++
#include<iostream>
#include<iomanip> // this is for changing how printed things look
int main() {
std::cout
<< std::setw(12) // set how many digits wide we want the field to take up
<< std::setfill('.') // set what to put in the blank space
<< std::left // align the number to the left
<< 123;
}

tired scarabBOT
#
Program Output
123.........
shell stirrup
#

like this?

#

This is a good example of what's known as an "X Y problem". You wanted to display an int in a certain way. You assumed the way to do it was to convert it into a string. You were having trouble converting it into a string, so you've come asking for help converting an int to a string, but your original problem was displaying the int.
When you're asking for help, it often helps to backtrack and give a more detailed explanation of the original problem that lead you down this path. You may find that the reason you're struggling to solve a problem is that you're looking at the wrong problem altogether

hasty bramble
#

OK i will drop you a link

#

one second @shell stirrup

#
.1.............
.2.............
3..............
4..............


..0............
..1............
..2............
.3.............
.4.............


...0...........
..1............
...2...........
.3.............
.4.............


...0...........
..1............
....2..........
.3.............
.4.............


...0...........
..1............
....2..........
..3............
..4............


...0...........
...1...........
....2..........
...3...........
...4...........


...0...........
...1...........
....2..........
....3..........
...4...........


...0...........
...1...........
.....2.........
.....3.........
...4...........


...0...........
...1...........
.....2.........
......3........
....4..........


...0...........
...1...........
......2........
.......3.......
....4..........


....0..........
...1...........
.......2.......
........3......
.....4.........


....0..........
...1...........
.......2.......
........3......
......4........


.....0.........
...1...........
.......2.......
.........3.....
......4........


.....0.........
....1..........
.......2.......
.........3.....
......4........


.....0.........
....1..........
........2......
..........3....
......4........


.....0.........
.....1.........
........2......
...........3...
.......4.......


.....0.........
......1........
.........2.....
............3..
.......4.......


.....0.........
.......1.......
.........2.....
............3..
.......4.......


.....0.........
........1......
.........2.....
............3..
.......4.......


.....0.........
........1......
..........2....
.............3.
.......4.......


......0........
.........1.....
...........2...
.............3.
.......4.......
Horse 3 wins!
#

to move riht as a "flip coin" function will determine if it moves right

#

i shorted the code becuase it wouldnt all fit but here is the main idea