C :: For Loop Skipping Statements

Jan 26, 2013

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.

Code:

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
}

[code]....

gets part for the line "Enter ID Number is skipped on the second iteration..

View 3 Replies


ADVERTISEMENT

C :: Breaking Out Of While Loop With Nested If Statements

Mar 14, 2014

Code:

while(x==1){
for (i=0;i<j;i++)
{if (word1[i] == word2[i])
{prefix[i]= word2[i];
counter++;}
else
x=2;}

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?

View 4 Replies View Related

C++ :: Print In Asterisks Oval / Arrow And Diamond Using For Loop And If Statements

Jan 15, 2015

#include <iostream>
using namespace std;
int main(){
return 0;
}

this is my main functions. I have problem making a program that prints in asterisks an oval, arrow and a diamond using for loop and if statements.

View 1 Replies View Related

C++ :: Skipping Cin Statement

Sep 24, 2013

I have written a C++ program in which a user need to input a character. My problem is it's working fine for the first time but as it goes through the code second time, it skips the cin statement and takes the previous defined value. how can i make cin statement work for the second time also?

View 5 Replies View Related

C++ :: Getline Skipping First Time

Apr 25, 2014

why does this happen that if i use

string ans;
for(int i=1; i<3; i++)
{
cout << "enter an option" << endl;
getline(cin,ans)
}

in a loop it always skips the first time but in the next times it asks?

View 3 Replies View Related

C++ :: Getline Skipping First Line?

Aug 21, 2014

text file is

hello
there

void readFile(char[], ifstream&);
address records[numbersAdd];
int main(){
char xml[30];
cout<<"Enter file name ";

[code].....

View 6 Replies View Related

C++ :: Function Call Is Skipping

Sep 13, 2013

When I call the member function in the main function, two functions are working fine but the third one(print()) is not. The program stops after executing the read_ages function and nothing printed on the screen. This is really strange and I could not find any problem after spending hours and had to post it here.It is a very simple program but I cant find the bug. It is a multiple file program and I am using MinGW as a compiler.

//File 1

#include<vector>
#include<string>
class Name_pairs {
private:
std::vector<std::string>names;
std::vector<double> ages;

[Code] ...

//I haven't made the sort function yet since I am stuck with the print(). Seems //like the compiler is skipping the print() function.

View 3 Replies View Related

C++ :: Compiler Skipping User Prompts?

Mar 24, 2014

the problem that I'm running into is that the compiler skips to the end when I put my initials in //User input > "Initials"

I'm using Microsoft Visual C++ 2010 Express.

Here is the assignment:

Plan and code a program to do the following. You found an exciting summer job for 5 weeks. It pays $15.50 per hour. You will input the number of hours per week that you worked and then compute your total earnings. You must pay taxes of 14%. After paying taxes, you will spend 20% of your money on cloths and 5% on school supplies. After buying clothes and supplies, you will use 25% of the remaining money for savings.

Input

Your 3 initials and the hours for each of the 5 weeks. Use the following numbers of hours for your first test 25, 30, 20, 23, 22.

Calculations

Gross pay is the rate of pay times the sum of all hours you worked. Use CONSTANTS for each of the following rates:

Tax rate 14% of the gross earnings
Clothing 20% of earnings after taxes
School supplies 5% of earnings after taxes
Savings 25% of earnings after taxes and expenses

Output:

Output your initials, total hours worked, gross earnings, taxes, net earnings after taxes, clothing expense, supplies expense, amount going to savings and amount left to spend. Output must be aligned to the right as shown with 2 decimals in all numbers. Sample output:

Initials ABC
Total Hours Worked 120.00
Gross Earnings 1860

Taxes paid 260.40
Net Earnings 1599.60

[code]....

Turn in:Be sure your output file contains user prompts and what was entered by the user. In addition to the results of your program processing. Run with above listed data.

Here is my code

#include <iostream>
#include <iomanip>
using namespace std;
int main() {
double Initials, TotalHours, GrossEarn, TaxesPaid;

[code]....

View 6 Replies View Related

C++ :: File Management - Skipping Certain Lines From Files

Mar 11, 2013

I am supposed to write a program that reads lines from files and see if the first character is an odd number, if so it will out put the remaining characters to output file, else itll seek to the beginning of the next line, this is what i came up with but its not working as it should, im not sure if its the version im using or the program I wrote, the commented part below was another algorithm I was trying to use.

#include<fstream>
#include<iostream>
using namespace std;
int main() {
char c;
int i =0;

[Code] ....

My text file was as follows:
input.txt
3Ali
4Sami
6Rashid
3Aya

output was unexpected , although some was relatively write but not as I wanted...

View 3 Replies View Related

C++ :: Skipping Pieces Of CSV File And Putting Into Array?

Apr 18, 2014

I am trying to put pieces of a csv file into an array, but i only want to put certain pieces of it. This is the information that i have

Player,Current Team, Age , Nat , Position ,From,To,Transfer fee
Gareth Bale, Real Madrid,24,Wales,RW,Tottenham,Real Madrid CF,91000000
Edinson Cavani,PSG,26,Uruguay,CF,SSC Napoli,PSG,64500000
Falcao, Monaco,27,Columbia,CF,Atlético Madrid,Monaco,60000000
Neymar, FC Barcelona,21,Brasil,LW,Santos,FC Barcelona,57100000
Mesut Özil, Arsenal,24,Germany,AM,Real Madrid ,Arsenal,50000000
James Rodríguez,Monaco, 21,Columbia,RW,FC Porto,Monaco,45000000

With this i want to skip the name and team but need age.

View 3 Replies View Related

C++ :: Convert Binary To Base10 - Array Initialization Skipping Straight To Int Main?

Mar 3, 2013

I am attempting to write a program that converts binary to base10, and vice versa.

But in the function for converting Base10 to Binary, just as it reaches the line of code

int* binary = new int [a];
it skips straight to the int main()

All I'm attempting to do with that line of code is initialize the variable "a" into the elements of the array "binary".

[URL] ....

View 5 Replies View Related

C :: Reading From A File And Skipping Lines To Find String In File

Nov 26, 2013

Program background: Ticket Sales Details You will sell tickets in advance and at the door. Prices for buying in advance and at the door will be given. Also, the total number of tickets sold in advance will be given. Each guest will have a unique number. If there are n tickets sold in advance, then these guests will be numbered 0 through n-1. As the event starts, requests to buy tickets at the door may be made and these guests will be numbered sequentially, starting at the lowest unassigned number. The maximum number of guests will be 1000. The first line of the file contains the following three values, separated by spaces: Cost of the presales tickets (in dollars), Cost of the tickets at the door (in dollars), and the number of presale tickets. The first two values will be positive real numbers to two decimal places and the last will be a positive integer.

The second line of the file will contain one positive integer representing the number of auction items followed by a positive real value to two decimal places (at most) representing the minimum bid increment, in dollars. The first value is guaranteed to be 1000 or less and the second will be in between 1 and 50, inclusive.

The third line of the file will contain all the prices of the auction items, in dollars, separated by spaces, in order. Thus, the first price is the price of item 0, the next price is the price of item 1, and so on. These values will be real numbers represented to up to 2 decimal places.

The fourth line of the file will contain the three following positive integers pertaining to the raffle: the number of raffle tickets available, the cost of a raffle ticket in dollars, and the number of raffle prizes. (It's strange to have raffle tickets that don't cost a whole number of dollars.)

The fifth line of the file will contain each of the values of the raffle items, in dollars, separated by spaces, in order. Thus, the first price is the price if item 0, the next price is the price of item 1, and so on. These values will be real numbers with upto 2 decimal places.

The sixth line of the file will contain ten positive integers representing the number of each of the drinks 0 through 9, in order, that are in stock.

The seventh line of the file will contain ten positive real numbers with upto 2 decimal places representing the price of each of the drinks 0 through 9, in order.

The eighth line of the file will contain a single positive integer, numEvents, representing the number of events that occur at the charity ball. These events are split into two groups: actions by guests at the ball and awards given (raffle, auction, person, totalrevenue). All of the actions precede all of the awards. You will produce exactly one line of output for each event described. Here are the formats of each event that could occur:

If a patron buys a ticket at the door, a command will be on a line by itself:

BUY TICKET k

where k is a positive integer indicating the number of tickets bought at the door. These guests will be numbered as previously mentioned. You are guaranteed that the total number of tickets bought, including presales, will not exceed 1000. This is what I have so far and I cannot figure out why it wont calculate the total revenue. I am not completely sure if it is even accessing the if statement in main.

Code:

#include <stdio.h> #include <stdlib.h>
#include <string.h>
#define N 1000

[Code].....

View 4 Replies View Related

C/C++ :: If Else Statements Being Ignored

Feb 7, 2015

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;

[Code] ....

View 2 Replies View Related

C :: Boolean Value Use For If And Else Statements

Apr 20, 2013

Am i using boolean values correctly? our professor wanted boolean value use for the if and else statements.

Code:
#include <stdio.h>
#include <math.h>
#define GRAVITY 9.8
#define LIMIT 500
#define SEC 5

[Code] ....

View 2 Replies View Related

C :: IF Statements Within Loops

Jan 29, 2014

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;
}

[code]....

View 4 Replies View Related

C++ :: How To Use If Statements With Strings

Jul 3, 2013

I've got a very simple but annoying problem.

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.

However, as of right now it's not working.

View 14 Replies View Related

C/C++ :: Using Strings And If Statements?

May 3, 2012

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?

#include <iostream>
#include <iomanip>
#include<stdlib.h>

[Code].....

View 1 Replies View Related

C :: If Statements For Dice Rolling

Oct 9, 2014

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."

I have written the program below:

Code:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
void main()
{
int d1,d2,sum;
d1=0;
d2=0;
}

[code]...

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??

View 1 Replies View Related

C :: Statements Within Braces Are Indented

Apr 25, 2013

When we say that the statements within the braces are indented, we mean a tab character distance? For example if we have :

Code:

// K&R Style
void some_function(void)
{
<1 tab>int x=1;
<1 tab>printf("Inside the function");
while(x<3) {
<1tab><1tab>printf("Inside the loop");
x++;
}
return;
}

That is right?

View 3 Replies View Related

C :: Operators In Switch Statements?

May 20, 2013

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.

View 7 Replies View Related

C++ :: If And Else Statements With Game Scores

Oct 20, 2014

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

View 4 Replies View Related

C++ :: Switch Statements In GLUT

Jan 3, 2014

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?

View 1 Replies View Related

C++ :: Randomizing Order Of If-else Statements

Dec 11, 2013

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.

View 18 Replies View Related

C++ :: How To Combine Inputs Using If Statements

Jul 19, 2013

How to combine the if statements?

cout<<"Enter a sale number"<<endl;
cin>>rec.sale;
if(cin.fail())
{
cout<<"Enter a number"<<endl;
cin>>rec.sale;
}

[Code]...

View 13 Replies View Related

C++ :: String Inputs On If Statements

May 26, 2014

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.

View 7 Replies View Related

C++ :: Execute The Previous Statements After Else Statement?

Mar 10, 2013

I have a condition and I would like to instead change the condition so that it would execute the previous statements after else statement.

It seems hard to explain but I'll try my best to illustrate anyway

if (condition)
<statement1>
else
<statement2>

So I would like to change the condition so that

if (condition)
<statement2>
else
<statement1>
And the condition is
Code:
if(!lightStateAtNextLink || !bIsLightEnAtConnection && nLinkNext.IndexToAttachedNode() != pVeh->_.stAutopilot.m_dwNextNode || bIsLightEnAtConnection && nLinkNext.IndexToAttachedNode() == pVeh->_.stAutopilot.m_dwNextNode)

And I have changed that to this. Is this correct?

Code: if(lightStateAtNextLink && bIsLightEnAtConnection || nLinkNext.IndexToAttachedNode() == pVeh->_.stAutopilot.m_dwNextNode && !bIsLightEnAtConnection || nLinkNext.IndexToAttachedNode() != pVeh->_.stAutopilot.m_dwNextNode)

View 5 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved