C/C++ :: For Loop Not Producing Correct Output
Nov 14, 2014
Something is wrong with option 1. For some reason it adds 1, 2, 3, 4 ... 5 onto the output and I can't think why. Here is a screenshot.
Also how can I make the output a little neater? I tried to use but it just looks horrible.
I had linked the source code! Why is there no edit button?!
#include<iostream>
using namespace std;
int main(){
int option;
cout << "Welcome to my program. Please select a following option.
[Code] .....
View 3 Replies
ADVERTISEMENT
Feb 4, 2014
I wanted to display the correct output by using a for loop at the end of my code, but for some reason it's not working correctly.
#include <iostream>
using namespace std;
int main() {
int* ipArray[3];
for(int i = 0; i < 3; i++) {
ipArray[i] = new int [5];
[Code] .....
What I wanted to do is pretty much match my 1st out put like 0 1 2 3 4 but my 2nd output only outputs 24's and I don't know why
View 11 Replies
View Related
Jul 14, 2014
Why my calculate function is producing a zero?I feel like it may have something to do with there being zeros the arrays it takes. The arbitrary cout statements are just for my debugging purposes. Input is formatted as follows:
<int>
<int>
<string>,<int>,<int>
<string>,<int>,<int>
<string>,<int>,<int>
<string>,<int>,<int>
and so on
#include <iostream>
#include <string>
#include <sstream>
int** parse_input(int &num_items, int &pouch_size);
int* calculate(int &num_items, int &pouch_size, int *values, int *weights, int &max_value);
[Code] ....
View 1 Replies
View Related
Oct 27, 2013
I am working on a project and decided to try something simple before I start adding items. I am calling a function from main and that function has a file pointer.
Here is my main.cpp
Code: #include <cstdio>
#include <string>
#include <iostream>
#include "main.h"
extern FILE *fp;
using namespace std;
int main(int argc, char *argv[])
[code]....
Here is the function:
Code:
#include <string>
#include <cstdio>
#include <iostream>
#include "main.h"
[Code] ....
My test file consists of several characters and digits. Nothing special and I at this point in time do not have any type of formatting that needs to be adhered to. I am simply wanting to read the file character by character and print it out. When I run the program, I get this symbol:
Code: If I use a printf statement, such as:
Code: printf("%s
", nextChar);
I get a segmentation fault.
My main.h Code:
#ifndef MAIN_H
#define MAIN_H
void scanner(FILE *);
//char getChar(FILE *);
#endif and lastly my scanner.h Code: #ifndef SCANNER_H
#define SCANNER_H
FILE *fp;
#endif
View 6 Replies
View Related
Feb 1, 2015
The program does compile the average and the mean correctly. I can't understand why the standard_deviation member function isn't applying the sqrt properly:
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
const int N = 10;
class Standard_Deviation {
[Code] ....
View 7 Replies
View Related
Feb 6, 2015
Ok so This is what the output of the program should be.
enter the count of input integers: 1
The count of the integers must be two or more. Please enter a valid number : 3
Enter integer #1 : 2
Enter integer #2 : -1
Enter integer #3:5
The positive numbers entered and their squares are:
2 4
5 25
My Code :
//header files
#include<stdio.h>
#include<ctype.h>
#include<string.h>
#include<math.h>
[Code].....
View 6 Replies
View Related
Oct 14, 2013
So I have to write a program that outputs a check with correct spacing and everything.
Create a project titled Lab7_Check. Write a program that asks the user the check information and prints the check. The dialog should be as follows:
date: 2/23/2010
name: William Schmidt
amount, dollars: 23
cents: 30
payee: Office Max
your check:
William Schmidt 10/13/2013
pay to: Office Max $23.30
twenty three and 30/100 dollars
You may assume that a person always has the first name and last name (no middle names or initials). The payee name is also always two words. The dollar and cent amount are integers and the amount is always less than 100 dollars. Note that the dollar amount could be zero, in which case, when you spell the dollar amount, it should print "zero". The date is always a single (non-white space separated string). Your date, dollar amount in numbers and the word "dollars" have to vertically align.
This is the code I have so far.
#include <iostream>
#include <string>
using namespace std;
int main(){
string date;
string firstname;
string lastname;
[Code] .....
View 3 Replies
View Related
Jan 27, 2015
I have a program that makes change from an amount. It works fine, but the output is the correct output looped over and over. I have tried everything, but it still doesn't work. For example, a amount of 98 should print
3 quarters
2 dimes
0 nickles
3 pennies
but instead it prints
3 quarters
2 dimes
0 nickels
3 pennies
0 quarters
2 dimes
0 nickels
3 pennies
0 quarters
2 dimes
0 nickels
3 pennies
Why it's doing this?
Code:
#include <iostream>
using namespace std;
int coinscount(int& amount, int value) {
int tracker = 0;
int amountdimes = amount;
[Code].....
View 3 Replies
View Related
Sep 3, 2014
My program works fine in all areas but adding the average every loop. It gives me a weird -1.#IND as an output, and it's supposed to calculate the total mileage each time I enter new values per trip.
// Automobile Mileage.cpp : Defines the entry point for the console application.
//
//Programmer: Ryan Youngen
[Code].....
View 1 Replies
View Related
Jul 21, 2013
is it possible to output like this using only one loop? if yes, how?
target output Code: ABCDE
EDCBA here is my code but using one loop im not getting my target output Code: #include <stdio.h>
main()
{
int x, y;
for(x='a', y='e'; x<='e'; x++, y--)
{
printf("%c
%c", x, y);
}
getch();
}
View 6 Replies
View Related
May 1, 2013
Code:
// Lab0Inventory.cpp : Starter lab
//Anastasia Glyantseva
#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <iomanip>
#include <sstream>
[Code]....
I can't figure out why my makeString gets called first and the pString its called with contains garbage. I want my allocateMem to get called first, but my program is not going to that. What is wrong with the order of my code?
View 3 Replies
View Related
Jan 17, 2012
I have done some programming or rather have written and implemented some algorithms in code (is there a difference? :-)) but am having some teething problems with a project I am currently working on.
To start with I am simply reading in the data from a .txt file into a vector. This is the code that I currently have:
Code:
#include <iostream>
#include <fstream>
#include <cerrno>
#include <vector>
[Code]....
This compiles fine but currently outputs a different integer everytime I run the program. I can remember having this problem when I started coding before using a different language and if I remember correctly it was quite easy to resolve. I thought it was because I had missed off the 'return 0' but it is something similar I think.
View 3 Replies
View Related
Jun 19, 2014
As I was testing it, the program suddenly stopped giving an output. Here is the code:
#include <iostream>
using namespace std;
int main() {
cout << "Ten people ate breakfast with you this morning. Each of them " <<
"had pancakes.
Please enter how many pancakes each of the ten people " <<
"ate.
" << endl;
[Code] ...
The while loop after int glutton does not work and seems to stop the program. I replaced the "glutton" in the cout with "testing testing" and it still did not work. However, when I commented out the loop, it appeared. Finally, it can't be what's inside the loop, because I commented that out too, and it still didn't work.
View 2 Replies
View Related
Nov 6, 2013
trying to output my struct to a for loop. I understand how to output the struct but not into a for loop.
#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;
struct Date {
int day;
int month;
int year;
[code]....
View 16 Replies
View Related
Feb 2, 2014
when you enter a name between 'p' and 's' it should send you to room 2432 but no matter what value you enter it says you can get your tickets here.
Code:
#include <stdio.h>
main()
{
[Code].....
View 2 Replies
View Related
Sep 25, 2014
In my program I am supposed to call isEqualTo with a user defined class type, and print out if my two numbers being compared are equal or not. I had to write two versions of this, one with just a template which works fine, but when I implemented the class, all the sudden my program just spews out true no matter if actually equal or not. Here is what I have so far:
#include "stdafx.h"
#include <iostream>
#include "UserClass.h"
using namespace std;
template<typename T>
bool isEqualTo(T value1, T value2){
if (value1 == value2)
[Code] ....
View 4 Replies
View Related
Jan 23, 2014
so i got this piece of code today having some slight errors with it, how its actually done as i want to know where i have gone wrong here is the code .. Using a for loop to input and output array values
*/
#include <stdio.h>
int main(void) {
/* Declare an array of integers */
int Grades[5];
int nCount;
/* Populate the array */
for(nCount = 0; nCount < 5; nCount++)
[Code]...
View 3 Replies
View Related
Feb 28, 2013
What does the following nested loop output ?
count = 1;
while (count <= 11) {
innerCount = 1
while innerCount <= (12 - count) / 2) {
cout << " ";
innerCount++;
} innerCount = 1;
while (innerCount <= count) {
cout << "@";
innerCount++;
} cout << endl;
count++;
}
View 9 Replies
View Related
Jul 30, 2014
[URL] when I try to write my output to a file. I am building a life insurance premium calculator with c++. What my calculator does is read in a text file called "policies.txt" containing a number of different policies. For example:
Pol1 M N 20 100000 1 .04 99
Pol2 F S 30 100000 1 .05 99
Pol3 M S 72 750000 1 .03 99
Pol4 F N 45 1000000 1 .05 99
And using this, it creates a single premium. Once the calculator calculates the premium of each policy, I want it to write out the premium amount of each policy to a textfile called "output.txt". The problem is that when I check my "output.txt" file, only the premium for the last policy shows up. It only reads:
Premium for Pol4 is 220384
When I want it to read:
Premium for Pol1 is 14101.6
Premium for Pol2 is 14221.2
Premium for Pol3 is 582391
Premium for Pol4 is 220384
How come only the last policy is showing up? and is there any way to make all four policies appear in my output text file? Here is my code:
double ratesmn[86] = {
#include "MaleNonSmoker.txt"
- 1
};
[Code]....
View 2 Replies
View Related
Mar 1, 2015
So I'm having trouble with my code. Got it to work, but having issues in terms of indenting my increment. For this example, I will use * as whitespaces.
Expected output:
0
*1
**2
***3
Your output:
0
*1
**2
***3
*****
The expected output has no extra whitespaces, but mine does. What am I doing wrong?
#include <iostream>
using namespace std;
int main() {
int userNum = 0;
int i = 0;
int j = 0;
for(i =0; i <= userNum; i++){
[Code] ....
View 5 Replies
View Related
Feb 21, 2013
How to fix this when i run the program and enter my name then output my name in vertical position using the for loop and press enter the program do not stop
#include<stdio.h>
#include<conio.h>
#include<dos.h>
main() {
char name,n;
clrscr();
printf("Enter Your Name:");
scanf("%c",&name);
[Code] ....
View 3 Replies
View Related
Nov 21, 2014
My loop is outputting data incorrectly. I have inbound web data that come in sets of 7. I am trying to in insert the 7 records into a vector and then display the vector content followed by a new line.
Code:
char buff[BUFSIZ];
FILE *fp = stdout;
char * cstr = buff;
vector<std::string> vrecords;
while(std::fgets(buff, sizeof buff, fp) != NULL){
for(int i = 0; i < 6; ++i){
[Code] ....
expected output:
Found buy!
198397652
2014-11-14 15:10:10
Buy
0.00517290
0.00100000
0.00100000
0.00000517
[Code] ....
View 14 Replies
View Related
Aug 31, 2013
I am trying to code a partition quicksort but the problem here is at the output is in an infinite loop:
The input : 4 3 5 7 2
The expected output : 2 3 4 5 7
But my output is infinite :
2 3 4 5 7
2 3 4 5 7
2 3 4 5 7
.
.
and so on
[Code:
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
/* Head ends here */
void partition(vector <int> ar) {
[Code] .....
View 5 Replies
View Related
Jun 12, 2014
Basically I'm supposed to use a while loop to generate a random number and use a switch statement to output the appropriate information. I feel like I'm missing a few things that are very simple.
The errors are:
warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data
warning C4700: uninitialized local variable 'randomNumber' used
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main(){
int i = 0;
unsigned int randomNumber;
[Code] .....
View 2 Replies
View Related
Jan 24, 2014
I have this code that im stuck on what i need to do is Extend the code such that when it starts the user is asked to input values to specify each of the three ranges before moving on to accept and tally the main values how do i do that Using a for loop to input and output array values Also calculate the average
*/
#include <stdio.h>
int main(void)
{
/* Declare an array of integers */
int Grades[5];
int nCount;
int nTotal = 0; /* Declare and initialise the value */
float fAverage;
[Code]...
View 1 Replies
View Related
Mar 9, 2015
I need to write a program to input 6 temperatures in degrees Fahrenheit and output it in degrees Celsius. I used a for loop but it keeps giving me 0 as the output whenever I enter a temperature...
Code:
#include <stdio.h>
#include <math.h>
int main() {
double farenheit,celsius;
for(int i=0; i<6; i++) {
printf("Enter temperature in degrees farenheit: ");
scanf("%lf",&farenheit);
celsius = (5/9)*(farenheit-32);
printf("Temperature in degrees celsius: %lf ",celsius);
} }
View 5 Replies
View Related