Hey all,

I am having a little bit of trouble wrapping this segment up.

I need a for loop that will wrap a 3 digit number (i.e. 320) vertically so that it displays like below.

3
2
0

instead of...

320

Here is the code I have already:

#include<iostream>
#include<fstream>
#include<cstdlib>
#include<cctype>
#include<string>
using namespace std;

 

int main()
{
//-----------------------------------------------------------------------------------------------------------------------------------------------
	//Filestream Object Declaration
	ofstream alphabet( "C:\\alphabet.txt" ) ;
//-----------------------------------------------------------------------------------------------------------------------------------------------
	//Variable Declarations
	string sentence ;
//-----------------------------------------------------------------------------------------------------------------------------------------------
	//User Input Prompt
	cout << "Please type a compilation of alphabet characters in no particular format below, press 'Enter' to count alphabet characters.\n" << endl;
//-----------------------------------------------------------------------------------------------------------------------------------------------
	//Capture User Input Store In string 'sentence'
	getline( cin, sentence );
//-----------------------------------------------------------------------------------------------------------------------------------------------
	//Error occurrence of file write to user
	if( ! alphabet )
	{
		cout << "Error occurred while attempting to open 'C:\\alphabet.txt'." << endl ;
		return -1 ; // Error signal occurrence
	}
//-----------------------------------------------------------------------------------------------------------------------------------------------
	//Successful occurrence of file write to user
	else
	{
		cout << endl;
		cout << "Character compilation saved successfully to 'C:\\alphabet.txt'!" << endl ;

	}
//-----------------------------------------------------------------------------------------------------------------------------------------------
// Write User Input To File 'C:\\alphabet.txt'
	alphabet << sentence << endl;
	//-----------------------------------------------------------------------------------------------------------------------------------------------
	// Close Filestream
	alphabet.close();
//-----------------------------------------------------------------------------------------------------------------------------------------------
	
	//***********************************************************************************************************************************************
	//***********************************************************************************************************************************************
	//***********************************************************************************************************************************************
	
	//Variable Declarations
	int count[26] = {0} ;
	char c ;
	int nonletter = 0 ;

//-----------------------------------------------------------------------------------------------------------------------------------------------
	//Input Filestream Object
	ifstream readAlphabet( "C:\\alphabet.txt" ) ;
//-----------------------------------------------------------------------------------------------------------------------------------------------
	//Error occurrence of file input to user
	if( ! readAlphabet )
	{
		cout << "Error occurred while attempting to open 'C:\\alphabet.txt'." << endl ;
		return -1 ; // Error signal occurrence
	}
//-----------------------------------------------------------------------------------------------------------------------------------------------
	//Successful occurrence of file input to user
	else
	{
		cout << endl;
		cout << "'C:\\alphabet.txt' successfully input to program!" << endl ;

	}
//-----------------------------------------------------------------------------------------------------------------------------------------------

	//***********************************************************************************************************************************************
	//***********************************************************************************************************************************************
	//***********************************************************************************************************************************************

	//PROCESS THE FILE
	readAlphabet.get(c);
//-----------------------------------------------------------------------------------------------------------------------------------------------	
	//WHILE LOOP
	while ( ! readAlphabet.eof() ) 
	{
		c = tolower(c) ; // Make all characters lower case
//-----------------------------------------------------------------------------------------------------------------------------------------------		
		if (c >= 'a' && c <= 'z')
		{
			count[c - 'a']++ ;
		}
//-----------------------------------------------------------------------------------------------------------------------------------------------
		else 
		{
			nonletter++ ;
		}
//-----------------------------------------------------------------------------------------------------------------------------------------------
		readAlphabet.get(c) ;
//-----------------------------------------------------------------------------------------------------------------------------------------------
	}//***END WHILE LOOP***
//-----------------------------------------------------------------------------------------------------------------------------------------------
	//Display Number Of Characters
	cout << endl ;
	cout << "Character counts: " << endl;
//-----------------------------------------------------------------------------------------------------------------------------------------------
	for( c = 0 ; c < 26 ; c++ )
	{
	
		cout << ( char )( 'a'+ c ) << " " ;

	}//***END ***
//-----------------------------------------------------------------------------------------------------------------------------------------------
	
	cout << "\n" ;

//-----------------------------------------------------------------------------------------------------------------------------------------------
	for( c = 0 ; c < 26 ; c++ )
	{
		
		cout << count[c] << endl << " " ;
	
	}//***END ***
//-----------------------------------------------------------------------------------------------------------------------------------------------
	cout << endl << count[1] ;
//-----------------------------------------------------------------------------------------------------------------------------------------------
	
	return 0;
}//***END main FUNCTION***

Edited 6 Years Ago by EngneerNitemare: n/a

I'm looking at what you want and the code you have and I came up with this.

#include <iostream>

using namespace std;

int main()
{
	int num;
	char buffer[20];
	cout << "Input a number: ";
	cin >> num;
	
	sprintf (buffer, "%d", num);
	
	string str = buffer;
	
	for( int i = 0; i < str.length(); i++ )
		cout << str[i] << endl;
	
	system("PAUSE");
	return 0;
}

This gives the output that you said you wanted in your post.

get rid of all the unneeded " //----// " it makes the code unreadable.

#include<iostream>
#include<fstream>
#include<cstdlib>
#include<cctype>
#include<string>
using namespace std;

 

int main()
{
	//Filestream Object Declaration
	ofstream alphabet( "C:\\alphabet.txt" ) ;
	//Variable Declarations
	string sentence ;
	//User Input Prompt
	cout << "Please type a compilation of alphabet characters in no particular format below, press 'Enter' to count alphabet characters.\n" << endl;
	//Capture User Input Store In string 'sentence'
	getline( cin, sentence );
	//Error occurrence of file write to user
	if( ! alphabet )
	{
		cout << "Error occurred while attempting to open 'C:\\alphabet.txt'." << endl ;
		return -1 ; // Error signal occurrence
	}
	//Successful occurrence of file write to user
	else
	{
		cout << endl;
		cout << "Character compilation saved successfully to 'C:\\alphabet.txt'!" << endl ;

	}
// Write User Input To File 'C:\\alphabet.txt'
	alphabet << sentence << endl;
	// Close Filestream
	alphabet.close();
	

	//Variable Declarations
	int count[26] = {0} ;
	char c ;
	int nonletter = 0 ;


	//Input Filestream Object
	ifstream readAlphabet( "C:\\alphabet.txt" ) ;

	//Error occurrence of file input to user
	if( ! readAlphabet )
	{
		cout << "Error occurred while attempting to open 'C:\\alphabet.txt'." << endl ;
		return -1 ; // Error signal occurrence
	}

	//Successful occurrence of file input to user
	else
	{
		cout << endl;
		cout << "'C:\\alphabet.txt' successfully input to program!" << endl ;

	}

	//PROCESS THE FILE
	readAlphabet.get(c);

	//WHILE LOOP
	while ( ! readAlphabet.eof() ) 
	{
		c = tolower(c) ; // Make all characters lower case

		if (c >= 'a' && c <= 'z')
		{
			count[c - 'a']++ ;
		}

		else 
		{
			nonletter++ ;
		}
		readAlphabet.get(c) ;		
	}

	//Display Number Of Characters
	cout << endl ;
	cout << "Character counts: " << endl;

	for( c = 0 ; c < 26 ; c++ )
	{
	
		cout << ( char )( 'a'+ c ) << " " ;

	}
	
	cout << "\n" ;

	for( c = 0 ; c < 26 ; c++ )
	{
		
		cout << count[c] << endl << " " ;
	
	}
	cout << endl << count[1] ;
	
	return 0;
}//***END main FUNCTION***

As for you function, you can do something like this :

#include<iostream>
#include<sstream>

using namespace std;

void print(char num){
	cout << num << endl;
}
void vertPrint(int num){
	
	stringstream convert;
	convert << num;
	string str;
	convert >> str;

	int cnt = 0;
	while(cnt < str.size()){
		print(str[cnt++]);
	}
}
int main(){
	vertPrint(123);
	return 0;
}

get rid of all the unneeded " //----// " it makes the code unreadable.

Yeah I know that all of the "unexecutable" lines would probably be annoying for some programmers. But, it actually helps me to separate the program visually into its individual parts. Still a noob.

As for you function, you can do something like this :

#include<iostream>
#include<sstream>

using namespace std;

void print(char num){
	cout << num << endl;
}
void vertPrint(int num){
	
	stringstream convert;
	convert << num;
	string str;
	convert >> str;

	int cnt = 0;
	while(cnt < str.size()){
		print(str[cnt++]);
	}
}
int main(){
	vertPrint(123);
	return 0;
}

Thanks for showing me this, it really helped me to understand the concept behind it! You sir are awesome! :D

Being as much of a newb as I am at C++ I am actually surprised that I was able to figure this one out.

Thank you everyone who responded btw!

Basically, I needed the code to print a number saved inside of an array.

I hope this helps those who are facing the same dilemma I was facing.

Cause NOW its something totally different!!!! lol :P

// PREPROCESSOR DIRECTIVES
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
#include<stdio.h>
using namespace std ;

int main()
{
	// Taken
	int a ;//
	int b ;//
	int c ;//
	int d ;//
	int i = 111 ;//
	int j = 222 ;//
	int n ;//
	int e ;//
	int f ;//
	int g ;//
	int h ;//
	int k ;//

	//Array count
	int count[3] = {532,345,279} ;
	int count2[3] = {0,1,2} ;
	

	a = i % 1 ;
	b = i % 10 ;
	d = i % 100 ;

	


  
  

	cout << "111 % 1 = " << a << endl ;
	cout << endl ;
	cout << "111 % 10 = " << b << endl ;
	cout << endl ;
	cout << "111 % 100 = " << d << endl ;
	cout << endl ;
	cout << endl ;


	
	[B]//Start for
	for( c = 0 ; c < 3 ; c++)
	{
		n = count[c];
		//Start if 1
		if( count[0] >= 100 )
		{
			cout << "Original element amount in count[" << count2[c] << "]: " << count[c] ;
			cout << endl ;
			cout << endl ;
			e = n % 100 ;// e = 532 % 100 == "32". *** e Prints '32' ***
			f = (n - e)/100 ;// f = (532 - 32)/100 == "5". *** f Prints '5' ***
			g = e % 10 ;// g = 32 % 10 == "2". *** g Prints '2' ***
			h = (e - g)/10 ;// h = (32 - 2)/10 == "3". *** h Prints '3' ***
			k = g ;// k = 2 == "2". *** k Prints '2' ***

			cout << f ;//1st or hundreds
			cout << endl ;
			cout << h ;//2nd or tens
			cout << endl ;
			cout << k ;//3rd or ones
			cout << endl ;

			
		}//End if 1

	}//End for [/B]

  return 0 ;
}
This article has been dead for over six months. Start a new discussion instead.