General

Chat

Basic While Loop Coding Question

Just learning some basic While Loop coding and have some practice questions to answer but I'm confused on this one.

[i]Write a program that uses a while loop. In each iteration of the loop, prompt the user to enter a number - positive or zero. Keep a running total of the numbers the user enters and also keep a count of the number of entries the user makes. The program should stop whenever the user enters "-1" to quit. When the user has finished, print the grand total and the number of entries the user typed.[/i]

Any help would be appreciated. Thanks.

October 9, 2013

8 Comments • Newest first

Momo123

Where did you guys learn programming? I took it twice in university and dropped it because it was way to hard and the professors sucked at teaching... Now I changed my major to Electrical Engineering

Reply October 10, 2013
ProBlades

@bloodIsShed: Ah, strings. Did not get to those yet; I thought I was missing something obvious in the basics. As for the illegality of equating phrases to numbers, I don't really need to do that if I can have it respond to a word, so thanks.

Reply October 10, 2013
SparklingCider

Observation: Maplers tend to study Computer Science.

Reply October 10, 2013
Zlyphor

Thanks everyone for the help! Managed to answer the question. @drager260: Thanks for the quick response.
@mcmuffinpimp: I'm taking Computer Science 20 at my school but we're using a textbook called Blue Pelican and Big Java.

Reply October 10, 2013 - edited
bloodIsShed

[quote=ProBlades]While we're on the topic of coding, anyone mind telling me how to make a program respond to a word and/or equate a word to a numerical value in C++?
For example, make the program respond to entering the word "basil", or make it think "basil"=1, or any other number. If this seems so simple it's dumb excuse me, I've JUST started learning coding so I don't know much.[/quote]

you could just read the string (=word) from the keyboard, as follows:
(arguably the easiest way)
#include<iostream>
#include<string>
int main()
{ std::string input;
std::cout<< "ntype a word here: ";
std::cin>> input;
std::cout<< "nYou entered " << input << "n";
return 0;
} now that you have the word in the variable input, you can make the program do whatever you want with it, anything legal that is. for example, you can use the string inside the if/else condition, in a loop condition, etc.
ex:
if (input == "fire&quot
{
std::cout<< "fire? where?";
}

as for the last part:
make it think "basil"=1, or any other number
that's illegal in most programming languages

you could "assign" a number to a string variable, but the program is really converting the number to a string if you do so.
there is however a way if you want to convert a number in string format to integer/double format

Reply October 9, 2013 - edited
ProBlades

While we're on the topic of coding, anyone mind telling me how to make a program respond to a word and/or equate a word to a numerical value in C++?
For example, make the program respond to entering the word "basil", or make it think "basil"=1, or any other number. If this seems so simple it's dumb excuse me, I've JUST started learning coding so I don't know much.

Reply October 9, 2013 - edited
bloodIsShed

above^^ input should be in double, or else the program will not work properly (imagine if the user types 3.14 for example)
in c++ it should be something like this:
#include <iostream>

int main()
{ double input = 0, sum = 0;
int count = 0;
while (input != -1)
{
std::cout << "Enter something: ";
std::cin >> input;
sum += input;
count++;
}
std::cout << "The total sum is " << ++sum
<< " and total number of entries is " << --count;
return 0;
}

Reply October 9, 2013 - edited
drager260

impot java.util.Scanner;
public static void main(String args[]){

Scanner s=new Scanner(System.in);
int count, user;
count = 0;

while(user != 1)
{ count++;
System.out.println("You have run this program " + count + " time(s)."

System.out.println("Would you like to run this again? Enter 1 for no and any other positive integer for yes"
user=s.nextInt();
} }

Don't know what language you wanted it in and I haven't programmed for a while but it should be something like this.

Reply October 9, 2013 - edited