In this program, I am trying to find the white space of my array, and store that value into a variable then print out that variable. The only function I know to use is the isspace one, and when I used it the error I recieve is: 'isspace' : cannot convert parameter 1 from 'char [80]' to 'int'
Any help would be greatly appreciated!
// Zachary Law Strings.cpp
#include <iostream>
using namespace std;
#include <string>
#include <iomanip>
int main()
{ int x, i,y;
char name[] = "Please enter your name: ";
char answer1 [80];
i=0;
y=0;
cout << name;
cin.getline(answer1, 79);
cout << endl;
x=strlen(answer1);
for (int i = 0; i < x; i++){
cout << answer1[i] << endl;
if (isspace(answer1))
{y=y+1;}}
cout << endl << endl;
cout << setw(80) << answer1;
cout <<y;
return 0;}
Each narrow character classification function takes an int argument that is either non-negative or the special value EOF. Otherwise the behavior is undefined. And with most C++ implementations char is a signed type, so that sufficiently high values (in effect, all characters outside ASCII) become negative.
So cast the argument to unsigned char, after adding the relevant indexing,
if( isspace( (unsigned char) answer1[i] ) )
Then the resulting non-negative value will be implicitly converted to int.
Instead of placing a cast in every invocation of a classification function, consider wrapping them in more C++-friendly fashion, e.g.
auto is_space( char const c )
-> bool
{ return ::isspace( (unsigned char) c ); }
try the below:
for (int i = 0; i < x; i++){
cout << answer1[i] << endl;
if (isspace(answer1[i]))
{y=y+1;}}
As I said earlier, you pass a array instead of a char to isspace function.
isspace function accept:
int isspace ( int c );
/* isspace example */
#include <stdio.h>
#include <ctype.h>
int main ()
{
char c;
int i=0;
char str[]="Example sentence to test isspace\n";
while (str[i])
{
c=str[i];
if (isspace(c)) c='\n';
putchar (c);
i++;
}
return 0;
}
Related
I want to make the program that counts the number of each digit of some number.
The number is multiple of three integer input from console. I convert the number to string for counting and used count function.
#include<iostream>
#include<algorithm>
#include<string>
#include<stdlib.h>
using namespace std;
int main() {
int a;
int b;
int c;
cin >> a;
cin >> b;
cin >> c;
int multi = a * b * c;
string str_multi = to_string(multi);
for (int a = 0; a <= 9;a++) {
char* tmp;
_itoa_s(a, tmp,2, 10);
cout << count(str_multi.begin(), str_multi.end(), tmp) << endl;
}
}
And I get the errorcode C2446.
The error saying,"'==':Not converted from 'const_TY' to 'int'"
This error was in the xutility file. I think the problem is count function or
_iota_s function but I don't know how to solve this.
The last parameter to std::count needs to be a char in this case (or be convertible to a char), so just change your loop to:
for (int a = '0'; a <= '9'; a++) {
cout << count(str_multi.begin(), str_multi.end(), a) << endl;
}
Live demo
I need to fill this array via user prompt. I was thinking to read in the user entry to a string and then assign that string to the array but that doesn't seem to be the right way to approach this. Could someone help me out?
The error I'm receiving reads "array type array[100] is not assignable"
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string.h>
using namespace std;
int main()
{
string theString;
char array[100]; // ARRAY MAX SIZE
int length = sizeof(array)-1;
char * ptrHead = array;
char *ptrTail = array + length - 1;
//USER PROMPTS & ARRAY FILL
cout << "Please enter a string to be reverse: " << endl;
cin >> theString;
array= theString;
//WHILE LOOP SWAPPING CHARACTERS OF STRING
while (ptrHead < ptrTail)
{
char temp = *ptrHead;
*ptrHead = *ptrTail;
*ptrTail = temp;
ptrHead++;
ptrTail--;
}
cout << array << endl;
return 0;
}
arrays are not assignable. You should use strcpy here:
But for this you'll have to convert theString to C like string.
strcpy(array, theString.c_str() );
Then adjust your ptrTail pointer too , like following :
int length = theString.size();
char *ptrTail = array + length - 1;
See Here
cin >> array; should put the input directly into the array, which I'm guessing is what you want
Also, there's a problem in your string reversal logic. You are reversing the entire array, not just the part that has been filled, which will put the filled portion at the end of the array. Consider using a function like strlen() to find out how long the actual input is.
You can copy the string to the array with strcpy or input the data directly to the array with cin >> array, but the better solution would be just not to use a char array, just use the string in your algorithm. That is also a better solution because you can overflow a fixed size char array
cout << "Please enter a string to be reverse: " << endl;
cin >> theString;
for (unsigned int i = 0; i <= theString.size() / 2; ++i)
swap(theString[i], theString[theString.size() - 1 - i);
cout << theString<< endl;
Edit
The same using pointers:
std::cout << "Please enter a string to be reverse: " << std::endl;
std::cin >> theString;
char* i = &theString[0];
char* j = &theString[theString.size() - 1];
for (; i < j; ++i, --j)
std::swap(*i, *j);
std::cout << theString << std::endl;
This program is supposed to compare the list of consonants to a user input list of letters and print out the number of consonants in the user's input. However, it just prints 0. I'm very new to C++ and am not experienced in finding logic errors.
#include <iostream>
#include <cctype>
#include <string>
using namespace std;
int counter(char *, char);
int main()
{
const int size = 51;
char input[size];
const char consonants[22] = "bcdfghjklmnpqrstvwxyz";
cout << "Enter your letters." << endl;
cin >> input;
cout << consonants << "appears";
cout << counter(input, consonants[22]) << "times" << endl;
}
int counter(char *strPtr, char ch)
{
int times = 0;
while (*strPtr != '\0')
{
if (*strPtr == ch)
times++;
strPtr++;
}
return times;
}
I'm aware you're new to C++, and this looks like some kind of exercise you are doing in order to learn, but I will post this answer so you can see how get this done using some of the C++ standar functions.
Using find function from algorithm
string test = "Hello world";
string vowels("aeiuo"); // Its much easier to define vowels than consonants.
int consonants_count = test.length(); // Assume all letters are consonants.
for (auto &c : test) // for each character in test
{
if (find(vowels.begin(), vowels.end(), c) != vowels.end()) // If c is founded inside vowels ...
{
consonants_count--; // Decrement the number of consonants.
}
}
Using regular expressions
#include <regex>
string test = "Hello world"; // A test string.
regex re("a|e|i|o|u"); // Regular expression that match any vowel.
string result = regex_replace(test, re, ""); // "Delete" vowels.
cout << result.length() << endl; // Count remaining letters.
Three problems:
You are not passing an array of consonants, you are passing a single character
You are passing an invalid character (one past the end of the consonant array)
You are counting how many times that invalid character is present.
To fix this problem, make sure that you pass an array as the second parameter, and add a nested loop to iterate that array.
your function counter if checking the input char by char and compare it to a single char(ch).
you need to run your counter function on all the chars in consonants array, or change the counter function:
int count = 0
for(int i = 0; i < 22 ; i ++)
{
count += counter(input, consonants[i])
}
now an even better way will be to count the non consonants characters and then do length-count
#include <iostream>
#include <cctype>
#include <string>
using namespace std;
int counter(char *, char);
int main()
{
const int size = 51;
char input[size];
cout << "Enter your letters." << endl;
cin >> input;
cout << consonants << "appears";
cout << counter(input) << "times" << endl;
}
int counter(char *strPtr)
{
int times = 0;
int length = 0;
const char consonants[5] = "aeoui";
while (*strPtr != '\0')
{
for(int i = 0; i < 5 ; i ++)
{
if (*strPtr == consonants[i])
times++;
strPtr++;
length++;
}
}
return length-times;
}
well I'm trying to get the user to input an integers as much as they want, until they input a negative number. 1st step was to use the ATOF function to convert string to number(which I did), and then allow the user to input integers(I only manage to do once just to see if I can use the atof function correctly.
Any help/tips is appreciated on giving me the right direction.
Here is my code thus far:
#include <iostream>
#include <string>
int main() {
using namespace std;
char buffer[256];
char tempBuff[256] = {'\n'};
double result;
int count = 0;
cout << "Testing " << endl;
cout << "Enter Any integers: ";
cin.getline(buffer,256);
for(int i = 0; i < strlen(buffer); i++)
{
if(isdigit(buffer[i]))
{
tempBuff[count] = buffer[i];
count++;
}
}
if (atof(tempBuff) > 0) {
result = atof(tempBuff) / 2;
}
cout << endl << "The integer you put was: " << tempBuff
<< " And dividing the integers "<< result << endl;
cin.ignore();
return 0;
}
How is atof supposed to know how many valid digits tempBuff contains? The atof function only accepts a C-style string as its input. Otherwise, it has no way to know how many characters are valid.
You can use tempBuff[count] = 0; before the call of atof. A C-style string is terminated by a zero byte.
This is probably a very quick fix, but I'm having trouble figuring out why I'm getting an error.
Code:
#include <iostream>
#include <queue>
#include <vector>
#include <iomanip>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main(int argc, char *argv[]){
srand ( time(NULL) );
double randomNumber = (double)(rand() % 100) / 100;
string numCars;
cout << "\nPlease enter the number of cars going through the intersection:" << endl;
cout << "->";
getline (cin, numCars);
for(double i=0; i<numCars; i++){
cout << randomNumber << endl;
}
}
The error is:
traffic.cpp:80: error: no match for ‘operator<’ in ‘i < numCars’
numCars is a string. It should have integer type (char, short, int, long)
You can't compare a string to a numeric value. Read the user input into an unsigned int. Change your code to:
unsigned int numCars;
if( !(cin >> numCars) ) {
// invalid user input, handle it
}
for( unsigned int i = 0 ; i < numCars; ++i ) {
// ...
}
I also changed the data type of i from double to unsigned int. There's no reason to use a floating point number unless somehow a fractional number of cars can pass through that intersection.
you can't compare string with integer or you have to define operator for this. Should numCars be integer?