EngneerNitemare 0 Newbie Poster

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] ; …
EngneerNitemare 0 Newbie Poster

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

EngneerNitemare 0 Newbie Poster

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 ; …
EngneerNitemare 0 Newbie Poster

Hey all,

I have this for loop and it keeps printing a '}' at the end of my alphabet and I can't figure out why.

Please help me figure out how to get rid of it! :)

Thanks!

- EngneerNitemare

// PREPROCESSOR DIRECTIVES
#include <iostream>
using namespace std ;

// main FUNCTION BEGIN PROGRAM EXECUTION
int main()
{
	// ALPHABET ARRAY DECLARATIONS
	int count[26] = { 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z' } ;
	count[0] = 'a' ; // 'a' Character = '97'
	count[1] = 'b' ; // 'b' Character = '98'
	count[2] = 'c' ; // 'c' Character = '99'
	count[3] = 'd' ; // 'd' Character = '100'
	count[4] = 'e' ; // 'e' Character = '101'
	count[5] = 'f' ; // 'f' Character = '102'
	count[6] = 'g' ; // 'g' Character = '103'
	count[7] = 'h' ; // 'h' Character = '104'
	count[8] = 'i' ; // 'i' Character = '105'
	count[9] = 'j' ; // 'j' Character = '106'
	count[10] = 'k' ; // 'k' Character = '107'
	count[11] = 'l' ; // 'l' Character = '108'
	count[12] = 'm' ; // 'm' Character = '109'
	count[13] = 'n' ; // 'n' Character = '110'
	count[14] = 'o' ; // 'o' Character = '111'
	count[15] = 'p' ; // 'p' Character = '112'
	count[16] = 'q' ; // 'q' Character = '113'
	count[17] = 'r' ; // 'r' Character = '114'
	count[18] = 's' ; // 's' Character = '115'
	count[19] = 't' ; // …
EngneerNitemare 0 Newbie Poster

Can someone please explain the concept behind bubble sorting a vector step by step? I know this is a tedious task so whoever answers I will much appreciate it! :)

Questions:

1.) What variables are being used?
2.) How and where is the function placed?
3.) Where and why are the for loops?
4.) Step by step procedure in example of vector bubble sort.

THANK YOU!

EngneerNitemare 0 Newbie Poster

I built this entire program and it does what I want it to except I want to use a bubble sort algorithm instead of the

std::sort()

function I have right now.

So please help me out on this. I don't really understand how bubble sorts work even after reading up on it.

Just a simple example of a bubble sort will do! Thanks!!! ;)

Here is my code with the std::sort().

:)

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
 
	int SIZE ; // Array size variable.
	int i ;
	int n = 0 ;
	int n2 = 0 ;

int main() // Start program execution.
{
	
		vector < int > Vector ( SIZE ) ; // Vector array declaration.

		cout << "Please insert array size: " ; // Prompt user for array SIZE variable input.
		cin >> SIZE ; // Get user chosen variable SIZE and store to variable int SIZE.

		cout << "Welcome to Array [" << SIZE <<"]." << endl ; // Print welcome note and SIZE of array.
		cout << endl ; //Prints blank line

		cout << "Unsorted below... " << endl ;

		for( i = 0; i < SIZE; i++ ) 
		{
			cout << "Array[" << n++ << "]: " ; // Print array name and element number
			Vector.push_back( rand() ) ; // Add random number inside each of the elements required
			cout << Vector.at(i) << endl ; // Print the random number added
		}

		cout << endl …
EngneerNitemare 0 Newbie Poster

Hey all!

I am trying to write a program that allows for:

1.) The user to enter the SIZE of an array.
2.) Prints random element numbers.
3.) Saves the random element numbers printed in the array.
4.) Bubble sorts and then prints the randomly printed numbers.

Here is what I have so far:

#include <iostream>
using namespace std;

	int * Array;
	int SIZE = 0;
	int n = 0;
	
void bubbleSort(int * Array,int SIZE)//Bubble sort function 
{
	int i,j;
	for(i=0;i<SIZE;i++)
	{
		for(j=0;j<i;j++)
		{
			if(Array[i]>Array[j])
			{
				int temp=Array[i]; //swap 
				Array[i]=Array[j];
				Array[j]=temp;
			}

		}

	}

} //End bubbleSort function


int main() 
{ 
	cout << "Please enter the \"SIZE\" (number of Elements) you want for this array: " << endl ; // Prompt user for SIZE
	cin >> SIZE; // Allow input of SIZE from user
	cout << endl; // Print blank line
	cout << "The \"SIZE\" (# of Elements) you have chosen for this array is: \"" << SIZE << "\" Elements." << endl ; //Print SIZE chosen to user
	cout << endl; // Print blank line
	cout << "Welcome to \"Array[" << SIZE << "]\"!" << endl; // Print welcome message
	cout << endl; // Prints blank line

	Array = new int[SIZE]; // Allocates memory for array

	cout << "The \"unsorted\" random Elements for \"Array[" << SIZE << "]\" are as follows:..." << endl;
	// Prints to user notification that the following is the unsorted Array SIZE elements
	cout << endl; //Prints blank …
EngneerNitemare 0 Newbie Poster

Hey guys thank you so much for the input on setting it up for user prompted array SIZE.

So far I have the program working the way I want it too until I get to the bubbleSort() algorithm.

Here is the code I got so far:

#include <iostream>
using namespace std;

	int * Array;
	int SIZE = 0;
	int n = 0;
	
void bubbleSort(int * Array,int SIZE)//Bubble sort function 
{
	int i,j;
	for(i=0;i<SIZE;i++)
	{
		for(j=0;j<i;j++)
		{
			if(Array[i]>Array[j])
			{
				int temp=Array[i]; //swap 
				Array[i]=Array[j];
				Array[j]=temp;
			}

		}

	}

} //End bubbleSort function


int main() 
{ 
	cout << "Please enter the \"SIZE\" (number of Elements) you want for this array: " << endl ; // Prompt user for SIZE
	cin >> SIZE; // Allow input of SIZE from user
	cout << endl; // Print blank line
	cout << "The \"SIZE\" (# of Elements) you have chosen for this array is: \"" << SIZE << "\" Elements." << endl ; //Print SIZE chosen to user
	cout << endl; // Print blank line
	cout << "Welcome to \"Array[" << SIZE << "]\"!" << endl; // Print welcome message
	cout << endl; // Prints blank line

	Array = new int[SIZE]; // Allocates memory for array

	cout << "The \"unsorted\" random Elements for \"Array[" << SIZE << "]\" are as follows:..." << endl;
	// Prints to user notification that the following is the unsorted Array SIZE elements
	cout << endl; //Prints blank line

	for(int i = 0; i < SIZE; i++)
    {
	   Array[i] = …
EngneerNitemare 0 Newbie Poster

Hey guys I have a couple of questions.

I am trying to build a program that will prompt for the user to enter the size of an array.

I've searched the web and several C++ books for guidance on this but have not been able to find anything of significant or easily understandable help.

So my questions are:

1. What code should be used to allow the user to enter the size of the array being used?

2. Will this code allow for the bubble sort algorithm?

Thanks everybody!

- Newb Programmer

EngneerNitemare 0 Newbie Poster

Student Problem:
"I am trying to get the code below to compile which is from my textbook "C++ How To Program (6th Edition)" Fig. 4.9.

However, I keep getting the "Fatal Error LNK1561: Entry Point Must Be Defined" every time I try to build the code."

What I Know So Far:
"My understanding of this error is that I do not have any main() to tell the compiler where to start the program."

How I Need Help:
"Please help me understand where I need my main() and why."

// Fig. 4.9: GradeBook.cpp
// Member-function definitions for class GradeBook that solves the
// class average program with counter-controlled repetition.
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include "GradeBook.h" // Include definition of class GradeBook
// Constructor initializes courseName with string supplied as argument
GradeBook::GradeBook( string name )
{
	setCourseName ( name ) ; // Validate and store courseName
} // End GradeBook constructor

// Function to set the course name;
// ensures that the course name has at most 25 characters
void GradeBook::setCourseName( string name )
{
	if ( name.length( ) <= 25 ) // If name has 25 or fewer characters
		courseName = name ; // Store the course name in the object
	else // if name is longer than 25 characters
	{ // Set courseName to first 25 characters of parameter name
		courseName = name.substr( 0,25 ) ; // Selects first 25 characters
		cout << "Name \"" << name << "\" …
EngneerNitemare 0 Newbie Poster

All code given still has not worked..

EngneerNitemare 0 Newbie Poster
#include <stdio.h>
#include <iostream>
using namespace std;

int main (int argc, char *argv[], char **env)
{
	char c, lastc = ' ' ;
	[B]c = toupper(cin.get());[/B]
	do {
		cout.put(c) ;
		c = cin.get() ;
	} while ( !cin.eof()) ;
	return 0;
}

Changed code,added "c = toupper(cin.get());" I believe I have tried this one before too. The program is still just simply typing out lowercase letters only!!!

This one is really frustrating me, I think that I am thinking too hard about it though.

Any other pointers???

EngneerNitemare 0 Newbie Poster

I have the following code which I have been trying get to output ALL capital letters using the toupper function.

My code will build successfully, however all the letters are still lower case when ever I type.

PLEASE HELP ME UNDERSTAND WHAT I AM DOING WRONG!

The following is the code I have gotten to so far, but I need to know where to place the toupper function. Everytime I try to associate it with char c I do something wrong.
-------------------------------------------------------------------------------------------

#include <stdio.h>
#include <iostream>
using namespace std;

int main (int argc, char *argv[], char **env)
{
	char c, lastc = ' ' ;
	c = cin.get() ;
	do {
		cout.put(c) ;
		
                c = cin.get() ;
	} while ( !cin.eof()) ;
	return 0;
}