How to make an input with a string thats part of an if statement. How I think it should be done :
#include <iostream>
#include <string>
using namespace std;
int main() {
string choose;
[Code] .....
But I get a strange warning (not an error, but something that is like "just letting you know" i suppose) and when i enter in ss or SS or aa or AA, it breaks my app.
I would like to understand a function on strings. Below is a code that I took from my teacher where the user inputs a string and prints out the length of the string.
Code: #include <stdio.h> #include <string.h> int main() { char str[100]; int i = 0;
[Code] ....
Now I understand that it returns the count in "int" so my question is:
Let's say i declared
Code: int count = 0; at the beginning of the code and then made Code: count = strlen(str); why wouldn't i have the same result? Is there a way to do it also?
My problem is that some of my conditions are not being triggered when the price level of a medium or large discount is smaller than the cost of the widget(defined as .40) An example being ,base price, $2 med discount 80%, and large discount 90%. My med discount is,1.6(80%) off of 2, so $.40, which is fine, but 90% off mean 1.8(90%) off of a base of $2, which is $.20 which is too low., and should trigger the 4th if statement, Nick you are discounting large purchases too much!. However when I run this in the program, I am triggering the first if statement.
#include <math.h> #include <stdio.h> #define widget_cost 0.40 int main (){ float sellprice; float discount_med_price;
I've just started learning the C language, and I'm stuck on something that is probably quite simple.how to get IF statements working within WHILE loops. My code is below. I am trying to write a program to count the number of words in a sentence, and obviously stop counting after a full stop has been entered. I created a variable called 'spaces' which will increase by one after the user enters a space. However, when the IF statement is in place, the loop never terminates, even if I enter a full stop. When I delete the IF statement, the loop functions correctly.
Code:
#include <stdio.h> int main() { char x; char a; char y; int spaces = 0; }
if (letter3==" "){ letter3==letter; PrintCharacterLineEnd();
This is a code. the string "letter3" contains the string " ". What I want to happen is when, letter3 contains the text " " I want to go to the function PrintCharacterLineEnd.
The problem with this code is that there are no errors showing, but when i compile the program, the if else statements do not carry out as they should do. Both if statements show the same answer, for example the second if statement. If I type Y or N then I get the same thing, 'You will now choose an event'. How do i get rid of this problem? Is char supposed to be used or string?
im a newbie C user and im having a little trouble in these for loop of mine im using. the first iteration is all fine but on the second and succesive iterations the first gets statement is skipped. im making a program that would ask the user to input multiple informations for atleast 5 people. i was also asked to use structures.. here is the code i have come up so far.. ive been stuck in it for like 3 hours now.
I'm very new to C Programming and am doing some homework for my intro to C class. "There is a game where one can play and bet money on. Two dice are rolled and the numbers that appear on the dice are added together. If the total is 7 or 11, then the user wins. If the total is 2 or 12, then the user loses. If a different total appears. then the user gets their money back."
However, after finishing the program, I realized that I NEEDED to use random numbers!!! How do I make my program generate random numbers within a range of 1-12? Also, how do I make my program generate NEW random numbers EVERY TIME instead of ONCE??
Switch case statements are a substitute for long if statements that compare a variable to several "integral" values ("integral" values are simply values that can be expressed as an integer, such as the value of a char).
So does that mean switch statements can only test if variable == value and nothing more, like > < >= <= != etc... ? I tried to make a program to test this and it seems like switch statements are limited to == but I'm not sure, maybe I'm doing something wrong.
This is the program I tried to make to test this:
Code: #include <stdio.h> int main () { int n;
[Code]....
So is it true that switch statements only work with the built in == operator? if that was the case then I would feel rather meh about switch statements.
I am in comp. Sci 1 at my school and I have to write a program that deals with gamer scores and the trophy they get according to expert level. For example Tom is a beginner and gets a gold medal if >=10000 a silver >= 7500 bronze if >= 5000 and no trophy if he score <5000 ! I have do this scoring for Beginner, Immediate and Expert.
Should I set up the beginning like this:
case'b': case 'B': if (score >= 10000 ) trophy= gold if (score >= 7500) trophy = silver if (score >= 5000) trophy= bronze else = none
Not really sure how to go about solving this problem
I'm making a game in OpenGL GLUT. A ball is going to move forwards along the Z axis and hit a wall. I want to be able to move the ball left and right, up and down before firing it off on the Z axis. I'm using glutSpecialKeys for this, and I've got everything set up, but I'm not sure how I use it with a switch statement? Here is a snippet of code:
void Special_Keys (int key, int x, int y){ switch(key){ case GLUT_KEY_UP: //do something break; case GLUT_KEY_DOWN: //do something
[Code] .....
Where the comment is saying "do something", I'm not sure what I actually need to do? Is it a method or what?
The purpose of doing this is so that the top of the if statements is not preferred over the bottom. I tried assigning enum values to each case. Then choose a random integer r from 0 to the size of the std::list myList containing those enum elements. The enum value is found using it = std::next (myList, r). Then if the if statement corresponding to that enum value is false, then myList.erase (it), and repeat the process with the newly reduce myList. It works, and everything seems nicely randomized. But it is disappointing much slower than when I used the original if-else statements (it is being applied hundreds of times).
Here is a snippet of my code (I decided not to use switch statements because it looked too clumsy):
std::list<FacingDirection> guyFacingDirections = {Positive_x, Negative_x, Positive_y, Negative_y, Positive_xPositive_y, Positive_xNegative_y, Negative_xPositive_y, Negative_xNegative_y}; while (true) { const int r = rand() % guyFacingDirections.size();
[Code] .....
There is a crowd of girls. Each guy will choose a girl, and then choose a facing direction to dance with his chosen girl. But not all facing directions are possible if someone is standing at the spot he wants to stand at to get his desired facing direction. Without randomizing the if-else statements, most of the guys will end up facing the same direction, which I don't like.
Basically after the 3rd run of the for loop, it encounters a contradiction. I want it to exit right there and then. Instead it continues to run the for loop. What can I do?
So I recently took on the task of making a 'Relativity Calculator' and am Having a trouble with some if statements. Here is the code.
// This program is designed to tell a user the Relativistic Length, mass and time of something going a certain speed under c
#include<iostream> #include<math.h> #include<stdlib.h> using namespace std; int main(){ char choice; // User's choice float c; //Speed of light
[Code] ....
My problem is that it says A, B, and Y are not defined, and theyre not, I have them set up so the user inputs them. But when I do put them in as char, The program compiles, but does nothing after i input A or B.
I'm havin trouble outputing different false statements in a boolean function... I'm currently working on a "secret number game" program which must generate a secret number and inform the user if his/her guess number is to high, to low or correct. I know boolean return true and false.. If the number is correct, the true statement will appear, if false... THAT'S where my problem starts cause now I have TWO statements to output..In a Function.. How do I make my program able to tell if the number guessed is "too high" or "too low" ?
I'm currently doing the exercises for the fifth chapter (Loops) and I've done all of them, but I wanted to go the extra mile on the last program I'm supposed to design. The program is a poll and all the input from the user will be with numbers. However when a letter is pressed then of course you get wrong behaviour from the program, it keeps looping endlessly.
Here is a fragment of what I think is the way of doing it - but of course it's not working
Code: int p = 0 char anyLetter[]={"abcde"};//Initializing char variable char a = anyLetter[p]; else if (userAnswer[n] == a)//if statement where char needs to be used. { cout << "Pressing a letter maybe? It's only with numbers. Try again." << endl; continue; }
/*Write a program that mimics a calculator. The program should take as input two integers and the operation to be performed. It should then output the numbers, the operator, and the result.(For division, if the denominator is zero, output and appropriate message.) Some sample outputs follow: