I'm new to c++, what i'm trying to do is count the amount of times a letter occurs with a piece of text or paragraph and stores this into an array called frequency array.
The code below is working to a degree, what happens is if the user types hello frequencyarray stores 11121, if the user types aaba frequency array stores 1213
I don't want a running total i'm wanting the array to store 1121 and 31. so if the same letter appears it adds 1 to the array.
Thanks David
#include <iostream> //for cout cin
#include <string> //for strings
#include <fstream> //for files
using namespace std;
int main()
{
string text;
int frequencyarray [26]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
cout << "Enter Word: ";
cin >> text;
//***************************COUNT OCCURANCES************************
for (int i(0); i < text.length(); i++)
{
char c = text[i];
c = toupper(c);
c -= 65;
if (c < 26 || c >=0)
{
frequencyarray[c]++;
cout << frequencyarray[c];
}
}
system ("pause");
return(0);
}`
If you don't want the running total, don't have the cout << freqencyarray[c]; inside the cycle that is counting the occurrences.
Try this
#include <iostream> //for cout cin
#include <string> //for strings
#include <fstream> //for files
#include <algorithm>
using namespace std;
int main()
{
string text;
int frequencyarray [26]={0};
cout << "Enter Word: ";
cin >> text;
//***************************COUNT OCCURANCES************************
for (int i(0); i < text.length(); i++)
{
char c = text[i];
c = toupper(c);
c -= 65;
if (c < 26 || c >=0)
{
frequencyarray[c]++;
}
}
std::for_each(std::begin(frequencyarray), std::end(frequencyarray), [](int i)
{
std::cout << i << ",";
});
std::cout << "\n";
system ("pause");
return(0);
}
Related
importing library
#include <iostream>
#include <cstdlib>
#include <stdlib.h>
#include <cmath>
#include <iostream>
#include <stdlib.h>
#include <cstring>
#include <math.h>
#include <stdlib.h>
#include <typeinfo>
using namespace std;
const int size = 100;
getting the user input for dollar amount
int main()
{
void mstold(char[]);
char array[size];
cout << "enter the amount" << endl;
cin >> array;
// cout<<strlen(array)<<endl;
mstold(array);
}
mstold function to extract only the digits and decimal from the user given dollar amount
void mstold(char array[])
{
int count = 0;
// char pure[strlen(array)];
int index_pure = 0;
// counting the number of significant values which are to be extracted
for (int i = 0; i < strlen(array); i++)
// if ((int(array[i]) < 58 and int(array[i]) > 48) or (array[i] == ','))
if ((int(array[i]) < 58 and int(array[i]) > 48) or (array[i] == '.'))
{
count++;
}
cout << "count" << endl;
cout << count << endl;
char pure[count + 1];
for (int i = 0; i < strlen(array); i++)
// if ((int(array[i]) < 58 and int(array[i]) > 48) or (array[i] == ','))
if ((int(array[i]) < 58 and int(array[i]) > 48) or (array[i] == '.'))
{
pure[index_pure] = array[i];
cout << int(array[i]) << endl;
++index_pure;
}
pure[index_pure] = '\0';
cout << "pure" << endl;
// created a array name pure which contains only digit and decimal.
// however this is an array.
// i need to convert this array of char to a double type variable.
cout << pure << endl;
// this line causing error
long double money = _atof_l(pure);
cout << money << endl;
// atoi is working and converting the string array to int
// however the array to double is not working
}
So I have built a small basic data encrypter (for learning purposes only). It is working perfectly fine but it reads only a single line of input. Is it my Editor problem or my code have some issues.
ps: I use CodeBlocks
#include <iostream>
#include <ctype.h>
using namespace std;
int main()
{
std::string str;
char enc;
int word;
cout << "\t\t\t\t\t\t\t\tENCRYPTOR" <<endl;
cout << "\t\t\t\t\t\t\t\t---------" <<endl;
cout << "Enter a Word: ";
getline(cin, str);
int n = 0;
cout << "\n\n\t\t\t\t\t\t\t\tENCRYPTED D#T#" <<endl;
cout << "\t\t\t\t\t\t\t\t--------------\n\n" << endl;
for(int i = 0; i < str.length(); i++){
int randomAdd[5] = {5,6,2,3,2};
int size = sizeof(randomAdd)/sizeof(randomAdd[0]);
// for(int j = 0; j < 5; j++){
word = str.at(i);
if(i%5 == 0){
n = 0;
}
enc = int(word) + randomAdd[n];
std::cout << char(enc);
n++;
}
return 0;
}
This works
Hello World
But I cannot enter this
Hello World
Have a nice day
because then the program exits command prompt without any error or message.
How can I read more than one line?
You can do as
#include <iostream>
using namespace std;
int main() {
string str;
while (getline(cin, str)) {
cout << str << endl;
}
return 0;
}
This code sample allows you to input multiple lines interactively from the command line/shell
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int main()
{
string str;
char enc;
int word;
vector<string> myInput;
cout << "\t\t\t\t\t\t\t\tENCRYPTOR" <<endl;
cout << "\t\t\t\t\t\t\t\t---------" <<endl;
while (str != "Enigma")
{
cout << "Enter a line (Write Enigma to exit input): ";
getline(cin, str);
myInput.push_back(str);
}
int n = 0;
cout << "\n\n\t\t\t\t\t\t\t\tENCRYPTED D#T#" <<endl;
cout << "\t\t\t\t\t\t\t\t--------------\n\n" << endl;
for(auto & myInputLine : myInput)
{
str = myInputLine;
for (size_t i = 0; i < str.length(); i++) {
int randomAdd[5] = { 5,6,2,3,2 };
int size = sizeof(randomAdd) / sizeof(randomAdd[0]);
word = str.at(i);
if (i % 5 == 0) {
n = 0;
}
enc = int(word) + randomAdd[n];
std::cout << char(enc);
n++;
}
}
return 0;
}
The input is finished if Enigma is written.
All input is stored in the vector container of the STL, see vector.
Afterwards, all the lines are encrypted by your algorithm.
Hope it helps?
I am a beginner and taking a CSC course, I have to write a program that converts a user input string into the sum of the ASCII value of each character, here is what I have so far, and Im still pretty far from being done. But any help would be greatly appreciated. Thanks
#include <iostream>
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
using std::string;
using std::cout;
using std::endl;
int main()
{
{
int x;
std::cout << "enter string" << std::endl;
std::cin >> x;
}
string text = "STRING";
for (int i = 0; i < text.size(); i++)
cout << (int)text[i] << endl;
return 0;
}
You can use a range-based for loop to traverse the string and then add up each char within:
#include <iostream>
#include <string>
int main()
{
int sum = 0; // this is where all the values are being added to
std::string s;
std::cout << "enter string and press enter." << std::endl;
std::cin >> s; // string that the user enters will be stored in s
for (char c : s)
sum += c;
std::cout << "total ASCII values: " << sum << std::endl;
return 0;
}
How do I display 3 names at a time, pausing to let the user press a key before the list continues displaying.
My code now only loops the first 3 values of the array
#include <iostream>
#include <string>
#include <iomanip>
using std::setw;
using namespace std;
int main() {
string a;
string names[10]; //size of array
for (int i = 0; i < 10; i++)
{
std::cout << "Enter name ";
std::cin >> a; //user input
names[i] = a; //assigns input to array
}
cout << "\n";
for (int k = 0; k < 10; k++)
{
for (int j = 0; j < 3; j++)
{
cout << names[j] << endl;
}
system("pause");
}
}
I changed the answer based on your comment. Instead of sleeping we just pause and wait until user inputs anything into the keyboard. Also a note, since you're using namespace, you don't need to include std::, I decided to use it since I was unsure what way you wanted.
#include <iostream>
#include <string>
#include <iomanip>
using std::setw;
using namespace std;
int main() {
string a;
int pauseCheck = 0; //new var
string names[10]; //size of array
for (int i = 0; i < 10; i++) {
std::cout << "Enter name ";
std::cin >> a; //user input
names[i] = a; //assigns input to array
}
cout << "\n";
for (int k = 0; k < 10; k++) {
cout << names[k] << endl;
pauseCheck++; //increments check
if (pauseCheck == 3) { //if equals 3
system("pause"); //we pause till user input
pauseCheck = 0; //reset pause check
}
}
system("pause"); //last and final pause before program ends
}
Here's another way of doing it which I think is a little more straight forward:
#include <iostream>
#include <string>
#include <iomanip>
using std::setw;
using namespace std;
int main() {
string a;
string names[10]; //size of array
for (int i = 0; i < 10; i++)
{
std::cout << "Enter name ";
std::cin >> a; //user input
names[i] = a; //assigns input to array
}
cout << "\n";
for (int k = 0; k < 10; k++)
{
cout << names[k] << endl;
if((k + 1) % 3 == 0) // everytime k + 1 is divisible by 3, let user hit a key
system("pause");
}
}
You can wait for a Enter-Press with another std::cin, just write into an garbage value.
I think other ways are not platform independent. You could ofcourse use the windows api, or unix stuff to get a key press.
Cant get to where I need to be. So I need a program using while loops only to get an output of . .
123456
12345
1234
123
12
1
Given, you enter the number amount of rows and in this case it is 6. My program now displays this
1
12
123
1234
12345
123456
Any help on inverting this? Program currently appears as . .
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int rows;
int i;
int j;
cout << "Enter number of rows: ";
cin >> rows;
int k=rows;
i=1;
while (i <= rows)
{
j=1;
while(j <= i)
{
cout << j%10;
j++;
}
cout << endl ;
i++;
}
}
Since you want to have a decreasing number of outputs you have to start your outer loop with the given input and decrease it for every iteration.
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int rows;
cout << "Enter number of rows: ";
cin >> rows;
int remainingRows = rows;
while (remainingRows >= 1)
{
int outputIndex = 1;
while(outputIndex <= remainingRows)
{
cout << outputIndex%10;
outputIndex++;
}
cout << endl ;
remainingRows--;
}
}
DEMO