:( handles a height of 0 correctly \ expected an exit code of 0, not output of "\n Please enter a positive integer valu..."
:( rejects a non-numeric height of "" \ expected output, not a prompt for input
https://sandbox.cs50.net/checks/5593ad8059ce4492804c07aff8e377eb
I think I should put part of my code too:
#include <stdio.h>
int clean_stdin()
{
while (getchar()!='\n');
return 1;
} //snippet gotten from http://stackoverflow.com/questions/14104013/prevent-users-from-entering-wrong-data-types
int main (void)
{
int row, pyramid_height, space, hash;
char c;
do
{
printf("\n Please enter a positive integer value, less than 24 as the height: ");
}
while (((scanf("%i%c", &pyramid_height, &c) != 2 || c!='\n') && clean_stdin()) || pyramid_height < 1 || pyramid_height > 23);
//snippet gotten from http://stackoverflow.com/questions/14104013/prevent-users-from-entering-wrong-data-types
Please help:
Also, Is there an easier way to prevent users from entering wrong data?
Thank you.
You should do something like this to ask the user for input
printf("Enter height < 23 and a non-negative number\n");
do{
printf("Height: ");
height = GetInt(); // ask user again until valid input is given
}while(height < 0 || height >23);
If you don't want to use GetInt() you can do this with scanf too. Just replace the GetInt line with scanf("%d",&height);. It'll work the same except when you enter a wrong number it'll yell at you by saying Height:
rather than Retry:.
And you should remove that clean_stdin function. That level of precision is not required in pset1.
Now the remaining part is the nested for loops which you've not provided in the question so, I am assuming that you have a problem there too since your program can't handle 0 properly.
Try something like this in place of the for loops.
for(int i=1; i<=height; i++){ // i number of #s in each step
for(int j=0; j<height-i; j++){ //print appropriate number of spaces
printf(" ");
}
for(int k=0; k<=i; k++){ //print #s
printf("#");
}
printf("\n"); //change line
}
Related
I know this is a fairly newbie question so I'm sorry if the solution is painfully obvious to you guys.
I've fully coded up pset 2 readability and it worked for printing out the number of letters, words and sentences for the user inputted text- I have since removed those print statement as they aren't needed for the pset (I just wanted to actually make sure the functions were returning something- they worked just fine).
I'm up to printing out the grade level now but no matter what text I input I only get before grade 1. I've already checked to see if I had anything wrong with my print statements and I can seem to find an issue there so I'm thinking that there may be an error in the calculation of the grade level itself- I've looked until my eyes have gone square and for the life of me I can not see anything wrong.
If someone could shed some light on my problem I would love to be saved the headache :), or even point me in the right direction so I get the learning. (also first time poster, long time lurkers so forgive me if anything is formatted incorrectly).
Thank you all!
Here is my code:
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
// my functions to calculate letters, words
int count_letters(string text);
int count_words(string text);
int count_sentences(string text);
int main(void)
{
string text = get_string("Text: ");
int letters = count_letters(text);
int words = count_words(text);
int sentences = count_sentences(text);
float calculation = (0.0588 * letters / words * 100) - (0.296 * sentences / words *
100) - 15.8;
int index = round(calculation);
if (index < 1)
{
printf("Before grade 1.\n");
}
else if (index >= 16)
{
printf("Grade: 16+.\n");
}
else
{
printf("Grade: %i.\n", index);
}
}
int count_letters(string text)
{
int letters = 0;
for (int i = 0; i < strlen(text); i++)
//((text[i] > 65 && text[i] < 90) || (text[i] > 97 && text[i] < 122))
if (isalpha(text[i]))
{
letters++;
}
return letters;
}
int count_words(string text)
{
int words = 0;
for (int i = 0; i < strlen(text); i++)
if isspace ((text[i]))
{
words++;
words = words + 1;
}
return words;
}
int count_sentences(string text)
{
int sentences = 0;
for (int i = 0; i < strlen(text); i++)
if (text[i] == '.' || text[i] == '!' || text[i] == '?')
{
sentences++;
}
return sentences;
}
I just wanted to actually make sure the functions were returning something- they worked just fine
Yes, the functions return "something" but is it the right thing? Suggest you add back the debug printf and look at the results carefully and critically. Start with the simplest text ("One fish. Two fish. Red fish. Blue fish."). 29 letters, 8 words, 4 sentences. What result is printed?
Inside of the function count_words
you have an if statement, if statements need to have brackets, the line wouldn't make sense even if you had the brackets, so double-check the logic as well.
I'm having some trouble trying to build a pyramid out of "#" for mario.c in problem set 1. My program seems to be having some trouble with handling input.
I get these errors:
:( handles a height of 1 correctly
expected ""#"", not "" #"\n"##""
:( handles a height of 2 correctly
expected "" #"\n"##"", not "" #"\n" ##"\n..."
:( handles a height of 8 correctly
expected "" #"\n" ...", not "" #"\n"..."
:( rejects a height of 9, and then accepts a height of 2
expected "" #"\n"##"", not "" #"\n" ##"\n..."
Here is my code:
#include <cs50.h>
#include <stdio.h>
int main(void)
{
int height;
do
{
height = get_int("How many rows for the pyramid?\n");
}
while (height < 1 || height > 8); //This loops until the user input 1 - 8 rows
for (int row = 0; row <= height; row++) //This says to print a new row until it reaches the user inputted height
{
for (int spaces = (height - (row + 1)); spaces >= 0; spaces--) //This formula prints less spaces as more rows are printed
{
printf(" ");
}
for (int hashes = 1; hashes <= (row + 1); hashes++) //This formula prints more hashes as more rows are printed
{
printf("#");
}
printf("\n");
}
}
Any help is appreciated!!
Your code prints an extra line of hashtags creating a pyramid which is 1 greater in height than the user input. You can see this in the error message you get:
:( handles a height of 1 correctly expected ""#"", not "" #"\n"##""
Hence, the output of your program is #\n## which equates to
#
##
The required output for a height of 1 is:
#
To fix your issue, you have to iterate through your code one lesser time than you do currently for any number. Hence, perhaps consider changing the condition: row <= height; in your for loop.
Consider changing the comparison to just <. By doing this, the code inside the for loop will run one lesser time.
I have been trying to make my pyramid from left aligned to right aligned but i am confused on how to do it. This is the code i am using.
Edit: i have changed the code but i have been getting an error
#include <cs50.h>
#include <stdio.h>
int main(void)
{
int height;
do
{
//asks user for number between 1 and 8
height = get_int("please give height: ");
}
while (height < 1 || height > 8);
//prints rows (i)
for (int rows = 0; rows < height; rows++)
{
//prints spaces (j)
for (int spaces = 0; spaces < height - rows; spaces++)
{
printf(".");
}
printf("\n");
}
for (int hashes = 0; rows < height - rows; hashes++)
{
printf("#");
}
printf("\n");
}
user gets prompted and writes number between 1 and 8
user types 4
Expected
...#
..##
.###
####
Actual output
....
...
..
.
mario.c:24:48: error: use of undeclared identifier 'rows'
for (int hashes = 0; rows < height - rows; hashes++)
^
mario.c:24:32: error: use of undeclared identifier 'rows'
for (int hashes = 0; rows < height - rows; hashes++)
^
2 errors generated.
<builtin>: recipe for target 'mario' failed
make: *** [mario] Error 1
i am trying to print hashes and use the rows interger but for some reason the error says it is am undefined interger.
I think the error is because you're curly brackets {} in your first and second for loops are mixed up. It looks like you're trying to do three loops that are nested inside each other; however, your third loops is outside the first one because you have an extra } in the middle of your code for the second loop. The variable row is declared in the first loop and the third loop doesn't know what that means since it is outside the first loop.
Sticking to the class's recommendations about indentation helped me keep this straight.
I am trying to count two binary numbers from string. The maximum number of counting digits have to be 253. Short numbers works, but when I add there some longer numbers, the output is wrong. The example of bad result is "10100101010000111111" with "000011010110000101100010010011101010001101011100000000111000000000001000100101101111101000111001000101011010010111000110".
#include <iostream>
#include <stdlib.h>
using namespace std;
bool isBinary(string b1,string b2);
int main()
{
string b1,b2;
long binary1,binary2;
int i = 0, remainder = 0, sum[254];
cout<<"Get two binary numbers:"<<endl;
cin>>b1>>b2;
binary1=atol(b1.c_str());
binary2=atol(b2.c_str());
if(isBinary(b1,b2)==true){
while (binary1 != 0 || binary2 != 0){
sum[i++] =(binary1 % 10 + binary2 % 10 + remainder) % 2;
remainder =(binary1 % 10 + binary2 % 10 + remainder) / 2;
binary1 = binary1 / 10;
binary2 = binary2 / 10;
}
if (remainder != 0){
sum[i++] = remainder;
}
--i;
cout<<"Result: ";
while (i >= 0){
cout<<sum[i--];
}
cout<<endl;
}else cout<<"Wrong input"<<endl;
return 0;
}
bool isBinary(string b1,string b2){
bool rozhodnuti1,rozhodnuti2;
for (int i = 0; i < b1.length();i++) {
if (b1[i]!='0' && b1[i]!='1') {
rozhodnuti1=false;
break;
}else rozhodnuti1=true;
}
for (int k = 0; k < b2.length();k++) {
if (b2[k]!='0' && b2[k]!='1') {
rozhodnuti2=false;
break;
}else rozhodnuti2=true;
}
if(rozhodnuti1==false || rozhodnuti2==false){ return false;}
else{ return true;}
}
One of the problems might be here: sum[i++]
This expression, as it is, first returns the value of i and then increases it by one.
Did you do it on purporse?
Change it to ++i.
It'd help if you could also post the "bad" output, so that we can try to move backward through the code starting from it.
EDIT 2015-11-7_17:10
Just to be sure everything was correct, I've added a cout to check what binary1 and binary2 contain after you assing them the result of the atol function: they contain the integer numbers 547284487 and 18333230, which obviously dont represent the correct binary-to-integer transposition of the two 01 strings you presented in your post.
Probably they somehow exceed the capacity of atol.
Also, the result of your "math" operations bring to an even stranger result, which is 6011111101, which obviously doesnt make any sense.
What do you mean, exactly, when you say you want to count these two numbers? Maybe you want to make a sum? I guess that's it.
But then, again, what you got there is two signed integer numbers and not two binaries, which means those %10 and %2 operations are (probably) misused.
EDIT 2015-11-07_17:20
I've tried to use your program with small binary strings and it actually works; with small binary strings.
It's a fact(?), at this point, that atol cant handle numerical strings that long.
My suggestion: use char arrays instead of strings and replace 0 and 1 characters with numerical values (if (bin1[i]){bin1[i]=1;}else{bin1[i]=0}) with which you'll be able to perform all the math operations you want (you've already written a working sum function, after all).
Once done with the math, you can just convert the char array back to actual characters for 0 and 1 and cout it on the screen.
EDIT 2015-11-07_17:30
Tested atol on my own: it correctly converts only strings that are up to 10 characters long.
Anything beyond the 10th character makes the function go crazy.
i want to create a pattern in c++ that looks like a trianlge(or half a diamond)
using asteriscks: the pattern should have 1, 2, 3, 4, and end in 5 stars like this
*
**
***
****
*****
(but straight!)
my code is as follows:
-`#include
using namespace std;
int main()
{
int size;
cout<<"size:"<<endl;
cin>>size;
int blank=size/2;
int newsize=1;
for (int i=0; i<=size/2; i++)
{
for(int j=blank;j>0;j--)
cout <<" ";
blank--;
for(int j=newsize; j>0; j--)
cout <<"*";
newsize+=2;
cout <<endl;
}
return 0;
}
`
my only problem with it is that it displays 1, 3,and 5 stars like this.
*
***
*****
its just a minor problem but although i have changed different parts of the code
i dont seem to get it right.
any suggestions?
thanks
:)
I'm not sure what you mean by "but straight", so I'll just ignore that for now...
Start with blank the same value as size, so that you can decrement the value each time without having to decrement by a half:
int blank=size;
Loop up to size instead of size/2 to get more lines:
for (int i=0; i<=size; i++)
Decrement by two in the loop for spaces to get half the number of spaces:
for(int j=blank;j>0;j-=2)
Increase the size by one instead of two to get the slower increase:
newsize++;
That should produce the output that you showed.
Edit:
I tested this to be sure, and the output is:
*
**
***
****
*****
******
To get the exact output that you asked for, start with blank one less:
int blank=size - 1;
Did I get it right: you want to place some asterisks on borders of character places? If so, it isn't possible. Every asterisk (or any other symbol), when displayed in monospace fonts, will reside in a middle of a character place, like in a grid. You can place asterisks inside the cells, but you cannot place asterisks on the borders of the grid.
int NUMLINES = 5;
void display(int, char);
void main(){
for (int i=1; i<= NUMLINES; ++i){
display((NUMLINES + 1 - i), ' ');
display(( 2 * i - 1 ), '*');
cout << endl;
}
}
void display (int howmany, char symbol){
for (int i = 1; i<=howmany; ++i)
cout << symbol;
}