C :: The Last User Entry In Loop Fprintfed Twice?
Apr 13, 2014
Still working on my first homegrown C program design . The function basically allows the user to enter a list of classes and grades and saves the list to a file to be used later in the file. The function compiles and runs through without error except for the fact that it always prints the last user entry to the *profilep file twice. Just as a note, the scanchar function is one I made to scan in one character and an end of line character to throw away the end of line char before I learned about %*c about 30 minutes ago...
Also I haven't much bothered to strengthen the function against crazy user input but I have heard using fgets and sscanf in conjunction can replace scanf and protect against weird user input. How to apply this within the program.
Code:
// creates a new profile and prints it to the profile file.
void newprof(FILE* profilep, const char *allclasses[ABBR_SIZE]){
int c, checker, counter;
int i, a;
char prof[MAX_PROF][ABBR_SIZE];
char grades[MAX_PROF][3];
[Code] .....
View 1 Replies
ADVERTISEMENT
Jul 6, 2014
Having multiple issue trying to get a while loop to stop after the third entry.
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
using namespace std;
int mySequentialSearch() {
int value;
string line;
[Code] .....
I am just working on the while loop right now. Then the function after I get the loop working.
View 10 Replies
View Related
Mar 7, 2013
I am trying to get this code eventually to read in a maze file to move the smiley face around in. But right now my current snag is the yes or no to enter the for loop.
#include <iostream>
#include <windows.h>
#include <conio.h>
#include <time.h>
using namespace std;
int main() {
int name;
char ans;
[Code] .....
View 3 Replies
View Related
Sep 5, 2014
I have a small loop that gets the user input, as well as acts as input validation to make sure he or she doesn't enter a value that is not within the specified range.
The user is supposed to enter a month number. E.g. if he or she chooses February, then enter 2, or December, 12.
It works correctly if they type a number that is not in the range, but goes into an infinite loop if, say, they type a string such as "month".
Code:
int main() {
// Variable Declaration(s)/Initialization(s)
int month=0;
// Get input
cout<<"Enter a month";
cin>>month;
[Code] ....
How could I go about revising this?
View 4 Replies
View Related
Jan 20, 2013
Some of my loop is not working. How messed up is this code.. Commented non working code
// ADIT.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include "windows.h"
[Code]....
View 3 Replies
View Related
Apr 14, 2013
This program is meant as practice. However, it is not working as intended. The for loop is meant to take the argument from the user and use it as the amount of times to loop. Instead it simply outputting: . It is supposed to output i++ but does not.
Here is the code:
main.cpp:
#include <iostream>
#include <stdlib.h>
int main(int main, char* arg[], char* arg2[]) {
int iterator = atoi(arg2[0]);
std::string z = "-c";
[Code] ....
View 19 Replies
View Related
Jul 31, 2013
I'm trying to get this program to loop the number of times I get it to input. The program compiles alright, and it does loop when I tell it too, but how do I output the grades of the multiple students?
#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
int weighted1 = 0;
int weighted2 = 0;
[Code] .....
View 12 Replies
View Related
Dec 5, 2013
struct stu_dat //outside main function
{
int rollno;
char name[45],
float average;
[Code] ....
No compilation problem.when executing prompt waits for inputting rollno, but, as soon as i enter a char string it keeps looping displaying the "want to enter more data?".i cant understand what is going on,as there is no compilation problem and runs good till i input the name.
View 3 Replies
View Related
Dec 13, 2014
In short, I'm trying to make a CLI for a project that can take multiple user inputs add I don't exactly know what I'm doing. I'm able to get cin to take one user input, but that's all I'm sure about.
View 8 Replies
View Related
Sep 15, 2014
I'm not sure what the program's purpose is?
1) Ask the user to enter a sentence. Until you reach the end of the sentence, test each character to count the number of spaces and letters.
2) For every space counted, print "SPACE", then print one "!" for every letter counted on an indented line underneath the "SPACE".
Your test sentence to use is: "The potash feldspars are important rock-forming minerals in plutonic, volcanic, and metamorphic rocks."
HINT: You do not need any strings. You need one while loop and two for loops. <cctype>
View 4 Replies
View Related
Sep 24, 2014
The purpose of this program is to unscramble the user's input. for example if the user entered 'ftooabll' the program would print 'football'. find the error that I am making....
Now, this only works for strings that are contained in the file wordlist. That being said, I would like this to repeat this search multiple times. Currently, the process is being repeated 7 times, but it only works on the first iteration. The code and sample input/output is below.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NUMLOOP 6
void sort_string(char*);
[Code] ....
Sample input/output: (note: all data has been verified to be in the wordlist)
Enter string:
4132dcba
abcd1234
Enter string:
4132dcba
Enter string:
4132dcba
Enter string:
4132dcba
Enter string:
4132dcba
Enter string:
4132dcba
Enter string:
4132dcba
View 3 Replies
View Related
Mar 6, 2015
So I have to write a step in my program that reads in up to 8 variables. Simple, but if the User submits an invalid value, I have to give an error message and ask him to re-enter the value until he enters a valid number. Here's my while loop that works perfectly:
Code:
while (true) {
printf("
Enter the mark given by Judge 1: ");
scanf("%f", &m1);
if ((m1 >= 0.0) && (m1 <= 10.0)) {
break;
}
[code]...
I'm not sure what to write in the else statement to make the programme understand that it has to repeat the exact same step but without adding 1 to index. Is this possible or do I have to suck it up and just use while loops instead? I'm very new to programming.
View 2 Replies
View Related
Apr 4, 2013
I'm making a program for a MMO I play. It calculates the cash and resources needed to build certain 'modules'. I'm having trouble to do something very simple : make a loop until the user asks to break the loop. Here is what the interface looks like :
What module do you want to install?
(Examples: 'Avenger' or 'VLLT')
dd // I input this
DD. COST each : 60,000 cr.
How many do you want? (Modules left:15)
(NOTE : You may enter a negative number if you made a mistake)
3 // I input this
Current Total Module Price : 180000 cr
+6 droids
[Code] ....
The bigger code box below is a part of the code I made. What I deleted is uninteresting, it's just the same thing again : other resources, other 'modules'...
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
struct resources// Defines the amount of resources and their price
[Code] ....
View 8 Replies
View Related
Aug 2, 2013
Write a program that computes a running sum of inputs from the user, terminating when the user gives an input value of 0
Using a while loop - no problem. Its only when I try to code it with a for loop that the program doesn't terminate with a 0 input. It terminates with a -1 input!!
while loop
#include <iostream>
using namespace std;
int main() {
float input=1;
float sum = 0;
[Code] ....
View 4 Replies
View Related
Aug 26, 2014
Keep track of the sum of values entered (as well as the smallest and the largest) and the number of values entered. When the loop ends, print the smallest, the largest, the number of values,and the sum of values. Note that to keep the sum, you have to decide on a unit to use for that sum; use cm.
View 9 Replies
View Related
Oct 21, 2014
How can i repeat a loop based on the number inputted by the user?
View 4 Replies
View Related
May 15, 2013
I'm trying to create a program that will take input from a user and calculate it in a do-while loop. The program does the calculation but the answer is wrong. The loop also doesn't work. The purpose of the program is to see how much an item will cost after a discount is taken off and tax is added.
#include <iostream>
#include <cmath>
using namespace std;
double original_cost;
double discount;
double tax;
double total;
char answer;
[Code]...
View 2 Replies
View Related
Feb 28, 2013
I am stuck on an exercise where i am supposed to use a loop to take user input and keep a running sum until the user enters a 0. the code i have so far is:
#include <iostream>
int main() {
using namespace std;
int num;
int total = 0;
int x;
[Code] ....
The full text of the error message is: error c2678:binary'>>':no operator found which takes a left-hand operand of type 'std::istream' . and one more thing i was wondering, is there a difference between c++ and visual c++?
View 4 Replies
View Related
Jul 29, 2013
I'm suppose to write a program using (for loop) that asks the user to enter any amount of numbers, so that it can display the smallest and largest. My program successfully finds the largest, but it is always displaying 0 for the smallest, I think Im doing something wrong with the internalization but I dont know what to change it to.
This is what I have ....
#include <iostream>
using namespace std;
int main() {
int amount;
int count;
int number = 0;
int smallest = 0;
int largest = 0;
cout << "Enter total numbers to process: ";
[Code] ....
View 4 Replies
View Related
Apr 24, 2012
We had to write a "selling program for computers, laptops and tablets", which I did but for the extra credit, we have to have those three points in the application and I have tried but how to do the "extra credit" part, which I really need.
1.) A loop to prompt the user if they would like to place another order
2.) At least one user-defined function
3.) An enumerated data type, array or struct (structure)
I did one of these three, it's a "DO WHILE" loop asking users if they want to make another order, it's right at the beginning of the code.
Code:
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
double desktop();//function prototype
double laptop();
double tablet();
[Code] ....
View 2 Replies
View Related
Mar 30, 2014
I'm trying to write a C++ program that will allow a user to input a number from the keyboard. Then using a loop, that will perform 10 times, multiply the entered number by the loop counter. Print out the loop counter, the entered number and the product of the loop counter and the entered number. A one-time heading should be displayed before information is printed.
This kinda of what I have so far:
#include <iosteam>
using namespace std;
int main () {
Start
Declare: numScores, sum, score, avg, SENTINEL = 200
numScores = 0
[Code] ....
All the programs I have tried to make are not working?
View 4 Replies
View Related
Apr 6, 2014
I am currently having problems creating a loop that will allow my user to choose to return to the beginning of the program or quit.
#include <iostream>
using namespace std;
int main() {
int j;
do {float a;
cout << "+----Welcome to Basic Operations----+
| Select Your Operation |
[Code] .....
I have not yet finished designing the interface for a couple of the operations, but right now i am hung up on trying to return to the beginning. I believe it is because the j was defined inside do and isn't carried out of the do statement for while.
View 13 Replies
View Related
Jan 28, 2015
Code:
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
int main(void) {
int i;
char *str;
int len = 1;
[Code]...
View 3 Replies
View Related
Feb 5, 2013
I have the following code in sourceFile.cpp. functionA() is first called and inserted entires for key1, key2 and key3. Then functionB() gets called to use the vectors for the 3 keys. I want to free all memory after exiting functionC(). Among the three ways to put an entry into the map for the 3 keys, which is correct / better?
Class ClassA { ... }
ClassA *key1 = new ClassA();
ClassA *key2 = new ClassA();
ClassA *key3 = new ClassA();
[Code]....
View 2 Replies
View Related
Nov 21, 2014
I have an assignment to create a program that will display the duplicate numbers that were entered by a user. I have the code finished and I have covered the basic requirements of the assignment. However, during my testing I found that if I enter anything other than a whole number (i.e. char or float) the program will run out and exit. I would like to add a little code to verify that the user has entered a whole number. I cannot seem to get this to work though. I have written other codes that verify if a number is above and below a certain value, I just do not know how to look for a certain type of entry.Here is my code:
int main() {
int number[20];
int duplicateNumber[20];
int i, j, k, counter = 0;
// Get number input from the user
for (i = 0; i < 20; i++) {
printf("Please enter a whole number %d: ", (i + 1));
[Code]...
View 3 Replies
View Related
May 3, 2013
I have to display an average for each student that is entered ....
void StudentData::displayAverageScore() {
double totalScore = 0;
double totalEx1 = 0, totalEx2 = 0, totalEx3 = 0, averageScore = 0;
for(int i = 0; i < entries.size(); i++) {
totalEx1 += entries[i].getEx1();// accumulate totals
totalEx2 += entries[i].getEx2();
totalEx3 += entries[i].getEx3();
if(totalScore != 0) averageScore = (totalEx1 + totalEx2 + totalEx3) / 3; // don't divide by 0
cout << "Average Score For Exams: " << setprecision(1) << fixed << averageScore << endl << endl;
}
}
View 7 Replies
View Related