C/C++ :: Writing Program For Class Where User Enters Their Age?
Aug 18, 2014
I am writing a program for a class where the user enters their age, how much money they have, and their full name. I know that I have to use the getline method to get the user's full name, and I know that I have to use cin.ignore() before that code, but i'm not sure why I have to use cin.ignore()?
This is the problem :- Write a program that keeps reading integers until user enters -1, then it prints the maximum and the minimum among all numbers (-1 should be ignored).
I have my Reverse Polish calculator compiling and everything but for the assignment I need to handle a few exceptions that I can't seem to get. First off I'm trying to make the program exit if the user enters only "0" but since the input i'm using is string, I cant figure out how to code
"If the first node is 0 and the next node = NULL, return true"
Here is my code: #include<iomanip> #include<iostream> #include<stdlib.h> #include<stdio.h> #include<string> #include<sstream> using namespace std;
My goal is to create a loop that keeps taking user input over and over until the user doesn't enter anything into 'cin >>', leaves the line blank, and simply presses the ENTER key to move on, at which point the program is supposed to break out of the loop and continue on with the rest of program execution. Something like this:
Code: do { cout << " Enter a name: "; cin >> input1; if (input1.empty())
[Code] ....
As you can see, I've already tried using the empty() function, but that didn't work, the program simply stays in the loop and doesn't break out, no matter how many times I press enter. It just keeps prompting me to enter a name. I've also tried using something like
Code: if (input1 == "")
but that doesnt work either. How do I break out of this loop?
So I've run into the following problem. My goal is to create a loop that keeps taking user input over and over until the user doesn't enter anything into 'cin >>', leaves the line blank, and simply presses the ENTER key to move on, at which point the program is supposed to break out of the loop and continue on with the rest of program execution. Something like this:
do { cout << " Enter a name: "; cin >> input1; if (input1.empty()) {
[Code] .....
As you can see, I've already tried using the empty() function, but that didn't work, the program simply stays in the loop and doesn't break out, no matter how many times I press enter. It just keeps prompting me to enter a name. I've also tried using something like
if (input1 == "")
but that doesn't work either. How do I break out of this loop?
im writing a program that asks the user to input a number 1-10 and will transform it into the roman numeral of that number but it seems that its not after the user inouts the number nothing will output
#include <iostream> #include<iomanip> using namespace std;
Ive been writing this code all day and these errors have been killing.
Instructions: Car Class: Write a class named Car that has the following: year. An int that holds the cars model year. make. A string object that holds the make of car. speed. An int that holds the cars current speed.
In addition, the class should have the following member functions.
Constructor. The constructor should accept the car's year and make as arguments and assign these values to a object's year and make member variables. The constructor should initialize the speed member variable to 0.
Accessors. Appropriate accessor functions should be created to allow values to be retrieved from an object's year, make, and speed member variables.
accelerate. the accelerate function should add 5 to the speed member variable each time it is called.
brake. The brake function should subtract 5 from the speed member variable each time it is called.
Demonstrate the class in a program that creates a Car object, and then calls accelerate function five times. After each call to the accelerate function, get the current speed of the car and display it. The, call the brake function five times. After each call to the brake function, get the current speed of the car and display it.
Errors: error C2061: syntax error : identifier 'stringm' error C2533: 'Car::{ctor}' : constructors not allowed a return type error C2511: 'Car::Car(int)' : overloaded member function not found in 'Car' see declaration of 'Car' fatal error C1903: unable to recover from previous error(s); stopping compilation
#include "stdafx.h" #include <iostream> #include <string> using namespace std;
My program gets user input and compares it against an array of characters (guessLetters[x]) to decide whether the do/while loop repeats. The whole character array contains underscores yet if i enter any letter it goes into the if statement and repeats the do/while loop.
If a user enters a string of boolean algebra it will ouput the table.
I have input parsing, cycling through the combinations, and outputing working. However once i parse the input I am not sure what to do with it. I have thought of having it write the parsed input to a new file as a function and then use that function, but that seems bad.
How to dynamically create the function, how to implement it.
BTW This is a console function, if that changes anything.
I'm new to coding, and I have to write a program to display a class schedule to the user (the classes are entered into the program in strings like this:
in the format of an excel file (the user picks the classes they want to have) that then displays the course title and professor in the correct cell corresponding to the day/time the class meets. I've been googling it, and from what I've read, I think I need to use a csv file, but I don't know how to input the data into the file. I know the general format for how data is entered, but I don't know how to write it into a specific file or if I even have to (we covered txt files very briefly in my class, but never touched csv files).
I'm doing a project. And I want to give the user the ability to write methods just like you would regularly in c# during runtime and then use them during runtime. Is such thing even possible? If so how?!
I am wanting to have a text file which is named with the user input and appended with .txt.
cout << "Please enter a new filename for storing new coordinates in: "; char name[50]; ofstream output; cin.getline(name, 50); output.open(name + ".txt");
program that I am working on. I want to use fgets() in my program so I could handle multiple words from a text(to be able to handle spaces). I get a weird result when running the program.
I'm currently finishing writing some small application. I want to be able to log important information about the program execution to a logfile, and I have several questions.
First of all - I'd prefer to make the part that logs information to a file separate from the code I've already written. So, what interface should I expose to the rest of the program? Is one function void log(const char*); enough?
Another thing that came to my mind; my program runs two threads, and I want to be able to write to the log file from both threads, so the question is: Should I make sure that the writing to the file is mutually exclusive?
And if so, whose responsibility is it to make the logging to the file thread-safe? The actual part that does the logging (void log(const char*) for that matter), or the parts of the program that calls log(const char*) ?
And lastly, and probably less importantly, where is it customary to save the logfile? (the user's home folder maybe?)
Writing a program to calculate grades... My algorithm is long, so I only posted the part that gives me trouble. When classes== 1,2,4, or 5, the program runs fine. but it terminates when classes == 3.
if (classes==3) { do { cout<<"Enter the Letter grade for 1st class. (USE CAPS)"<<endl; cin>>grade1;
I have been asked to write a program to grade several multiple-choice exams. The exam has less than 80 questions, each answered with a letter in the range of ‘a’ through ‘f’. The data are stored on several files such as exam1.dat where the first line is the key, consisting of a string of n characters (0<n < 80). The remaining lines on the file are exam answers, and consist of a student ID number, a space, and a string of n characters.
The program should have a while loop in the main routine to ask users input a data file name through keyboard. In this way, the program has a capability of repeatedly asking for a new data file in each while loop until users input a key word “exit”. Once “exit” is encountered, the while loop terminates and your program ends. A typical input exam data file (exam1.dat) looks like:
Apply strlen( ) or the length( ) of string to the first line of the above data file for determining the number of questions in the problem. If a student gives more answers than necessary, the extra answers will be automatically truncated. On the other hand, if a student provides less number of answers, the remaining unanswered questions are considered as being answered wrongly.
After users input an exam data file, your program should ask users to input another grade-curving file name (e.g., gradeCurving.dat). This second file contains the information to convert a percentile score to a curved grade in levels of ‘A’ through ‘E’. For instance, a grade-curving file takes the following format: a curved alphabetic grade, a space, a percentile grade served as marker.
A 90 B 80 C 70 D 60 E 50
The above information means that ‘A’ = 90 through 100; ‘B’=80 through 89; ‘C’=70 through 79; ‘D’ = 60 through 69; “E”=50 through 59; For the remaining grades, you can assign an ‘F’.
Furthermore, in each while loop in the main routine, your program should ask users to input an output file name such as score1.dat. The output file will store the scores for each student in a format: student ID number, a space, a percentile score, and a curved grade in ‘A’ though ‘E’. The program should also compute and display the following statistics for the graded answers: Average score, Maximum score, and Minimum score.
A typical output on a data file looks like:
1234567 90% A 9876543 85% B 5554446 95% A 4445556 75% C 5551112 80% B Statistics: Average Score: 85% Minimum Score: 95% Maximum Score: 75%
This Is what I have so far. It compiles fine and everything but when I input the files it says "There was an error opening the corresponding files. Check input name perhaps?" and it exits out ....
writing a program that requires exception handling. if an error occurs, i what the program to go back to the begging of the loop. i tried using break but that just makes the program crash when it receives a bad input. how do i do this? this is what i have so far (this part of the program any ways)
while (! quit) { // Output phone book menu cout << endl
I am having some trouble with my class assignment. We need to write a C program that will calculate the employee salary. There are 5 employees that will need to be calculated. We are required to use a loop also.
So far this is what I have but I am receiving errors when running the program.
#include <stdio.h> int main() { int clock_num; /* employee clock number */ float gross; /* gross pay for week (wage * hours) */ float hours; /* number of hours worked per week */ float wage; /* hourly wage */ /* Prompt for input values from the screen */ printf ("This is a program to calculate gross pay.
I'm writing a program that finds the probability of moves a Knight can make on a chess board. The program asks the user to enter the row and the column and the program is supposed to output how many possible moves the Knight can make, but the output is wrong. Example(if I enter row 1 and column 1 the output is 8 and it should be 2) I know the problem is with my if statements.
#include <iostream> using namespace std; int chess(int a, int b) { int x =a; int y = b; int sum=0;
i need the output to display the product of every whole number from 1-3 in a table format.
this is the code i have so far. i know some brackets are missing but i just pasted the meat of the code. when i run the program i keep getting the number 1 displayed in a straight line going on forever
write a program that computes the factorial of a number and displays it. A factorial of a number (written N!) is defined as N * (N-1) * (N-2) * (N-3) ... *1 In other words, 5! = 5 * 4 * 3 * 2 * 1 = 120 and 3! = 3 * 2 * 1 + 6.
Example of output 15 is 1.30767e+012
Can't get it to display error when user enters a number lower than 2 or higher 60.
// Program to calculate factorial of a number #include <iostream> #include <iomanip>